C#代码:
CODE:
1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Web; 5 using System.Web.Security; 6 using System.Web.UI; 7 using System.Web.UI.WebControls; 8 using System.Web.UI.WebControls.WebParts; 9 using System.Web.UI.HtmlControls; 10 using System.Data.SqlClient; 11 12 public 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 }
Html代码:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 2 3 4 5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 6 7 <html xmlns="http://www.w3.org/1999/xhtml" > 8 <head runat="server"> 9 <title>无标题页</title> 10 </head> 11 <body> 12 <form id="form1" runat="server"> 13 <div> 14 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> 15 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"> 16 <Columns> 17 <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" 18 ReadOnly="True" SortExpression="ProductID" /> 19 <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" /> 20 <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" /> 21 </Columns> 22 </asp:GridView> 23 </div> 24 </form> 25 </body> 26 </html>
数据库连接字串:
<connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=FENGWEI;Initial Catalog=Northwind;User ID=sa;Password=1234" providerName="System.Data.SqlClient"/> </connectionStrings>