如何在datatable vb.net中添加新行 [英] how to add new rows into a datatable vb.net
问题描述
- 用户可以在文本框上写下名称,然后点击添加按钮。它将保存在带有自动ID的数据表中。
- 之后,文本框被清除,用户可以在文本框中另行写一个名称,然后点击按钮。
- 这应该添加到现有ID = 1的内存上的datatable。
- 在gridview上显示。 (这只是为了显示目的来确认它的工作原理)
我有一个这样的datatable。
Button1.click()事件
Dim name = txtname.Text
Dim dt作为新的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 =
目前我有一段时间像上面的代码。在真实的程序中,它不仅仅是名字,它不只是一个数据,所以我只是模拟一些代码。
这个代码为aspx。
< asp:TextBox ID =txtnamerunat =server>
< / asp:TextBox>< asp:Button ID =Button1runat =serverText =Button/>
< asp:GridView ID =GridView1runat =server>
< / asp:GridView>
任何人都可以建议我怎么做?我明白我的代码是无效的,但不正确,但我只需要放一些代码,这样你就不需要为我从头开始工作了。
非常感谢
这是一个向第一列使用AutoIncrement的数据表添加新行的示例:
定义表结构:
Dim dt As New DataTable
dt。 Columns.Add(ID)
dt.Columns.Add(Name)
dt.Columns(0).AutoIncrement = True
添加新行:
Dim R As DataRow = dt .NewRow
pre>
R(Name)= txtName.Text
dt.Rows.Add(R)
DataGridView1.DataSource = dt
I have a form with a textbox and a add button.
- user can write a name down on the textbox and click add button. it will save in a datatable with auto ID.
- after that, the textbox is cleared and the user can write another name on the text box and click the button.
- this should add to the existing datatable on memory with existing ID + 1.
- show on the gridview. (this is just for display purpose to confirm it works)
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.
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.
Thanks so much.
解决方案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
这篇关于如何在datatable vb.net中添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!