0

任何人都可以帮助我如何在 asp 中的 gridview 中添加新记录,这是我的代码:

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim drow As DataRow
    Dim dt As New DataTable
    Dim x As Integer

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        drow = dt.NewRow
        drow("fname") = TextBox1.Text
        drow("mname") = TextBox2.Text
        drow("lname") = TextBox3.Text
        dt.Rows.Add(drow)

        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            binds()
        End If
    End Sub
    Protected Sub binds()

        dt.Columns.Add("fname")
        dt.Columns.Add("mname")
        dt.Columns.Add("lname")
        drow = dt.NewRow
        drow("fname") = TextBox1.Text
        drow("mname") = TextBox2.Text
        drow("lname") = TextBox3.Text
        dt.Rows.Add(drow)

        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub
End Class

当我单击按钮时,错误表明 fname 不属于该表。谢谢

4

3 回答 3

0

原因是所有对象都在页面生命周期结束时被释放,包括 yourDataTableDataRowineteger。它们将在每次回发时使用其默认值进行初始化。

但是你有这个初始化代码Page_Load

If Page.IsPostBack = False
    binds() ' which creates the table and adds the columns '
End If

解决方案不是使用字段,而是使用局部变量。

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    binds()
End Sub

Private Sub binds()
    Dim drow As DataRow
    Dim dt As New DataTable
    dt.Columns.Add("fname")
    dt.Columns.Add("mname")
    dt.Columns.Add("lname")
    drow = dt.NewRow
    drow("fname") = TextBox1.Text
    drow("mname") = TextBox2.Text
    drow("lname") = TextBox3.Text
    dt.Rows.Add(drow)

    GridView1.DataSource = dt
    GridView1.DataBind()
End Sub
于 2013-02-27T13:18:47.400 回答
0

这是我的代码。

部分类 _Default

Inherits System.Web.UI.Page
Dim dcl As DataRowCollection
Dim drow As DataRow
Dim dt As New DataTable

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    binds()

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Page.IsPostBack = False Then
        addcolumns()
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub

Private Sub addcolumns()

    dt.Columns.Add("fname")
    dt.Columns.Add("mname")
    dt.Columns.Add("lname")
    Session("dtInSession") = dt

End Sub
Protected Sub binds()

    If Session("dtInSession") IsNot Nothing Then
        dt = Session("dtInSession")
    End If

    drow = dt.NewRow
    drow("fname") = TextBox1.Text
    drow("mname") = TextBox2.Text
    drow("lname") = TextBox3.Text
    dt.Rows.Add(drow)

    Session("dtInSession") = dt
    GridView1.DataSource = dt
    GridView1.DataBind()

End Sub

结束类

于 2013-02-27T14:09:29.820 回答
-2
## Heading ##
    public DataTable SearchStudents(string studFirstname)
    {
        logOBj.conn.Close();
        logOBj.conn.Open();

       logOBj.DA = new SqlDataAdapter("Select * From STUDENT where studFirstname LIKE '%" + studFirstname + "%'", logOBj.conn);

       logOBj.DT = new DataTable();
       logOBj.DA.Fill(logOBj.DT);//data adapter fills the datatable

       return logOBj.DT;
    }
于 2015-03-25T16:40:43.003 回答