将多行添加到gridview中

时间:2010-03-29 07:27:36

标签: asp.net

在按钮单击中逐个添加行.. (我尝试过,但是它在同一行上覆盖)

3 个答案:

答案 0 :(得分:0)

应将gridview绑定到数据源。您需要向此数据源添加一条记录,然后调用myGridView.Bind()来绑定它。

关于GridViews的MSDN上有很多信息:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.aspx

答案 1 :(得分:0)

你可以这样做......

   static DataTable dt;

        private void Form1_Load(object sender, EventArgs e)
        {
             dt = new DataTable();
            dt.Columns.Add("Colum1");
            dt.Columns.Add("Colum2");

        }

        private void button1_Click(object sender, EventArgs e)
        {
            dt.Rows.Add(textBox1.Text, textBox2.Text);
            dataGridView1.DataSource = dt;

        }

答案 2 :(得分:0)

这是你的vb。经过测试,似乎工作正常。将dt声明为共享

Imports System.Data

Partial Class _Default
    Inherits System.Web.UI.Page

 Shared dt As DataTable


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            dt = New DataTable
            dt.Columns.Add("Colum1")
            dt.Columns.Add("Colum2")

        End If

    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        dt.Rows.Add(TextBox1.Text, TextBox2.Text)
        GridView1.DataSource = dt
        GridView1.DataBind()

    End Sub
End Class