如何将新行添加到数据表 vb.net [英] how to add new rows into a datatable vb.net
问题描述
我有一个带有文本框和添加"按钮的表单.
I have a form with a textbox and a "add" button.
- 用户可以在文本框中写下名称,然后单击添加"按钮.它将保存在具有自动 ID 的数据表中.
- 之后,文本框被清除,用户可以在文本框中输入另一个名称并单击按钮.
- 这应该添加到内存中现有 ID + 1 的现有数据表中.
- 然后在网格视图中显示.(这只是为了显示目的,以确认它有效)
我有一个这样的数据表.
I have a datatable like this.
Button1.click() event
Dim name = txtname.Text
Dim dt As New DataTable
dt.Columns.Add("ID", GetType(Integer))
dt.Columns.Add("Name", GetType(String))
Dim N As Integer = dt.Columns("ID").AutoIncrement
dt.Rows.Add(N, name)
GridView1.DataSource = dt
GridView1.DataBind()
txtname.Text = ""
目前我有一些时间像上面的代码.在实际程序中,它不仅仅是名称,也不仅仅是一个数据表,所以我只是模拟了一些代码.
At the moment I have sometime like the code above. in the real program, it is not just name and it is not just one datatable so i just mock up some code.
以及此 aspx 代码.
and this code for aspx.
<asp:TextBox ID="txtname" runat="server">
</asp:TextBox><asp:Button ID="Button1" runat="server" Text="Button" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
谁能建议我怎么做?我知道我的代码很烂而且不正确,但我只需要放一些代码,这样你们就不需要为我从头开始工作.
can anyone advice me how to do it ? i understand my code is crapped and not correct but i just have to put some code so that you guys not need to work from scratch for me.
非常感谢.
推荐答案
以下是向第一列使用 AutoIncrement 的数据表添加新行的示例:
Here is an example of adding a new row to a datatable that uses AutoIncrement on the first column:
定义表结构:
Dim dt As New DataTable
dt.Columns.Add("ID")
dt.Columns.Add("Name")
dt.Columns(0).AutoIncrement = True
添加新行:
Dim R As DataRow = dt.NewRow
R("Name") = txtName.Text
dt.Rows.Add(R)
DataGridView1.DataSource = dt
这篇关于如何将新行添加到数据表 vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!