how to add new rows into a datatable vb.net

后端 未结 2 719
你的背包
你的背包 2021-01-01 11:07

I have a form with a textbox and a \"add\" button.

  1. The user can write a name down on the textbox and click the \"add\" button. It will save in a datatable wit
相关标签:
2条回答
  • 2021-01-01 11:28

    First You need to define the data table structure as like the following:

    Dim dt As New DataTable
    dt.Columns.Add("ID", Type.GetType("System.String"))
    dt.Columns.Add("Name",Type.GetType("System.String"))
    

    And Then add row like:

    Dim dr As DataRow = dt.NewRow
    dr("ID") = System.GUID.NewGUID()
    dr("Name") = txtName.Text
    dt.Rows.Add(dr)
    DataGridView1.DataSource = dt
    DataGridView1.DataBind()
    
    0 讨论(0)
  • 2021-01-01 11:30

    Here is an example of adding a new row to a datatable that uses AutoIncrement on the first column:

    Define the table structure:

        Dim dt As New DataTable
        dt.Columns.Add("ID")
        dt.Columns.Add("Name")
        dt.Columns(0).AutoIncrement = True
    

    Add a New row:

        Dim R As DataRow = dt.NewRow
        R("Name") = txtName.Text
        dt.Rows.Add(R)
        DataGridView1.DataSource = dt
    
    0 讨论(0)
提交回复
热议问题