• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
守望麦田
博客园    首页    新随笔    联系   管理    订阅  订阅

动态添加gridview行(转自oec2003的学习专栏)

c# code:
Code
 1using System;
 2using System.Data;
 3using System.Configuration;
 4using System.Web;
 5using System.Web.Security;
 6using System.Web.UI;
 7using System.Web.UI.WebControls;
 8using System.Web.UI.WebControls.WebParts;
 9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11
12public partial class _Default : System.Web.UI.Page 
13{
14    protected void Page_Load(object sender, EventArgs e)
15    {
16        if (!this.IsPostBack)
17        {
18            BindGrid();
19        }

20    }

21
22    private DataTable ReadGridView()
23    {
24        DataTable dt = new DataTable();
25        DataRow dr;
26        dt.Columns.Add(new DataColumn("ProductID", typeof(string)));
27        dt.Columns.Add(new DataColumn("ProductName", typeof(string)));
28        dt.Columns.Add(new DataColumn("CategoryID", typeof(string)));
29        for (int i = 0; i < this.GridView1.Rows.Count; i++)
30        {
31            dr = dt.NewRow();
32            dr[0] = this.GridView1.Rows[i].Cells[0].Text.Trim();
33            dr[1] = this.GridView1.Rows[i].Cells[1].Text.Trim();
34            dr[2] = this.GridView1.Rows[i].Cells[2].Text.Trim();
35            dt.Rows.Add(dr);
36        }

37        return dt;
38    }

39    protected void Button1_Click(object sender, EventArgs e)
40    {
41        DataTable dt = ReadGridView();
42        //this.GridView1.DataSource = dt;
43        //this.GridView1.DataBind();
44        DataRow row = dt.NewRow();
45        row.ItemArray = new object[] { "oec2003","oec2003","oec2003" };
46        dt.Rows.InsertAt(row, 0);
47        dt.AcceptChanges();
48        this.GridView1.DataSource = dt;
49        this.GridView1.DataBind();
50    }

51
52    private void BindGrid()
53    {
54        string str = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString();
55        using (SqlConnection con = new SqlConnection(str))
56        {
57            SqlCommand cmd = new SqlCommand("SELECT top 1  [ProductID], [ProductName], [CategoryID] FROM [Products]", con);
58            SqlDataAdapter sda = new SqlDataAdapter(cmd);
59            DataSet ds = new DataSet();
60            sda.Fill(ds);
61            this.GridView1.DataSource = ds.Tables[0].DefaultView;
62            this.GridView1.DataBind();
63            sda.Dispose();
64            ds.Dispose();
65        }

66
67    }

68}

69

HTML code:
Code
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID">
            
<Columns>
                
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
                    ReadOnly
="True" SortExpression="ProductID" />
                
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
                
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />
            
</Columns>
        
</asp:GridView> 
    
</div>
    
</form>
</body>
</html>

数据库连接字符串:
<connectionStrings>
        
<add name="NorthwindConnectionString" connectionString="Data Source=FENGWEI;Initial Catalog=Northwind;User ID=sa;Password=1234" providerName="System.Data.SqlClient"/>
    
</connectionStrings>
posted @ 2009-09-04 12:16  守望麦田  阅读(175)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3