vb.net如何将文本从文本框添加到datagridview

问题描述:

大家好

如何将文本框中的文本添加到列(0)中的datagridview中

how to add text from textbox into datagridview in column(0)

 

如果DataGridView没有设置DataSource属性,则以下内容将在第一行中插入文本列

If the DataGridView does not have the DataSource property set the following will insert text into the first row, first column

DataGridView1.Rows(0).Cells(0).Value = TextBox1.Text

如果已设置数据源,例如您已经将DataTable加载到BindingSource中,然后将BindingSource设置为DataGridView的DataSource,然后我们将进行当前行,第一列是用于演示的第一列是针对具有字段名称的Text 的名字.

If the DataSource is set, e.g. you have loaded a DataTable into a BindingSource then set the BindingSource as the DataSource of the DataGridView then we would do the current row, first column were the first column for demoing is for Text with the Field Name of FirstName.

声明绑定源

Private bsData As New BindingSource

这里dt是我们的数据表

Here dt is our DataTable

bsData.DataSource = dt
DataGridView1.DataSource = bsData

设置值

CType(bsData.Current, DataRowView).Row.SetField(Of String)("FirstName", TextBox1.Text)

CType(bsData.DataSource, DataTable).Rows(DataGridView1.CurrentRow.Index).SetField(Of String)("FirstName", TextBox1.Text)