動態添加GridView行

來源:互聯網
上載者:User

c#代碼:

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        if (!this.IsPostBack)        {            BindGrid();        }    }    private DataTable ReadGridView()    {        DataTable dt = new DataTable();        DataRow dr;        dt.Columns.Add(new DataColumn("ProductID", typeof(string)));        dt.Columns.Add(new DataColumn("ProductName", typeof(string)));        dt.Columns.Add(new DataColumn("CategoryID", typeof(string)));        for (int i = 0; i < this.GridView1.Rows.Count; i++)        {            dr = dt.NewRow();            dr[0] = this.GridView1.Rows[i].Cells[0].Text.Trim();            dr[1] = this.GridView1.Rows[i].Cells[1].Text.Trim();            dr[2] = this.GridView1.Rows[i].Cells[2].Text.Trim();            dt.Rows.Add(dr);        }        return dt;    }    protected void Button1_Click(object sender, EventArgs e)    {        DataTable dt = ReadGridView();        //this.GridView1.DataSource = dt;        //this.GridView1.DataBind();        DataRow row = dt.NewRow();        row.ItemArray = new object[] { "oec2003", "oec2003", "oec2003" };        dt.Rows.InsertAt(row, 0);        dt.AcceptChanges();        this.GridView1.DataSource = dt;        this.GridView1.DataBind();    }    private void BindGrid()    {        string str = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString();        using (SqlConnection con = new SqlConnection(str))        {            SqlCommand cmd = new SqlCommand("SELECT top 1  [ProductID], [ProductName], [CategoryID] FROM [Products]", con);            SqlDataAdapter sda = new SqlDataAdapter(cmd);            DataSet ds = new DataSet();            sda.Fill(ds);            this.GridView1.DataSource = ds.Tables[0].DefaultView;            this.GridView1.DataBind();            sda.Dispose();            ds.Dispose();        }    }}
html代碼
<!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 id="Head1" runat="server">    <title>無標題頁</title></head><body>    <form id="form2" runat="server">    <div>        <asp:Button ID="Button3" 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>

AddRow.rar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.