How to fill a DataGridViewComboBoxColumn? - vb.net

Sorry if this is something that should be obvious, but this is my first project using VB.NET, and some things are still beyond me.
I'm trying to setup a combobox inside a DataGridView but I keep getting
System.ArgumentException: DatagridviewComboBoxCell value not valid
I've been googling this for 2 hours now and to me it seems I'm setting up thing correctly, but probably something is amiss.
Dim imageCol As DataGridViewImageColumn
Dim checkCol As DataGridViewCheckBoxColumn
Dim col As DataGridViewColumn
Dim comboCol As DataGridViewComboBoxColumn
Dim ds As DataSet
Dim som As New SomStructure
Dim somministrazioni() As SomStructure = {}
With dgvListaAttivita
.Columns.Clear()
.AutoGenerateColumns = False
.ReadOnly = False
.EditMode = DataGridViewEditMode.EditOnEnter
.CausesValidation = False
somministrazioni.Clear
ds = getSomministrazioni(codevalue, Today())
If ds IsNot Nothing Then
For Each row As DataRow In ds.Tables(0).Rows
som.idOspite = row(0)
som.nomeOspite = row(1)
som.descrizioneSomministrazione = row(2)
som.idOperatore = row(3)
som.nomeOperatore = row(4)
som.preparata = False
somministrazioni.Add(som)
Next
End If
.DataSource = somministrazioni
imageCol = New DataGridViewImageColumn
imageCol.Width = 25
imageCol.ImageLayout = DataGridViewImageCellLayout.Normal
imageCol.Description = "delete"
imageCol.Image = My.Resources.note
.Columns.Add(imageCol)
col = New DataGridViewColumn
col.DataPropertyName = "descrizioneSomministrazione"
col.HeaderText = "Somministrazione"
col.ValueType = GetType(String)
col.CellTemplate = New DataGridViewTextBoxCell
col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
col.ReadOnly = True
.Columns.Add(col)
comboCol = New DataGridViewComboBoxColumn
Dim dt As DataTable = dsOperatori.Tables(0)
comboCol.DataSource = dt
comboCol.DisplayMember = "display"
comboCol.ValueMember = "idoperatore"
comboCol.DataPropertyName = "idOperatore"
comboCol.HeaderText = "Operatore"
comboCol.ValueType = GetType(Integer)
comboCol.CellTemplate = New DataGridViewComboBoxCell
comboCol.Width = 150
.Columns.Add(comboCol)
checkCol = New DataGridViewCheckBoxColumn
checkCol.DataPropertyName = "preparata"
checkCol.HeaderText = "P."
checkCol.ValueType = GetType(Boolean)
checkCol.CellTemplate = New DataGridViewCheckBoxCell
checkCol.Width = 20
.Columns.Add(checkCol)
End With
As I understand it it should be like this:
comboCol.DataSource = dt
comboCol.DisplayMember = "display"
comboCol.ValueMember = "idoperatore"
specifies the datasource and key/display values of the comboboxes in the column
comboCol.DataPropertyName = "idOperatore"
Is the name of the column linked to both the DataSource of the DataGridView and the ValueMember of the ComboBox, and that should display different selections on different rows.
If I remove the DataPropertyName from the code I don't get the error anymore, but I also get empty comboboxes.

Try commenting out the template line:
comboCol = New DataGridViewComboBoxColumn
Dim dt As DataTable = dsOperatori.Tables(0)
comboCol.DataSource = dt
comboCol.DisplayMember = "display"
comboCol.ValueMember = "idoperatore"
comboCol.DataPropertyName = "idOperatore"
comboCol.HeaderText = "Operatore"
comboCol.ValueType = GetType(Integer)
'comboCol.CellTemplate = New DataGridViewComboBoxCell
comboCol.Width = 150
.Columns.Add(comboCol)

Related

DataGridView Yes No ComboBox Column

I have what on the face of it seems a pretty simple requirement, I need an unbound combobox column to hold an option list of 'Yes' and 'No'. In combobox parlance Yes and No are the DisplayMembers corresponding to ValueMembers 1 and 0 respectively which in turn are the values held in the field of the database table. I use bound combobox columns in the application extensively and am very familiar with building them, but can't for the hell of me work out how to incorporate this simple list functionality in the datagridview application. Could somebody please put me out of my misery with some sample code.
Thanks for your feedback Plutonix. The DGV is bound to an underlying table. Hopefully, the following code should give you some idea of what I am attempting to acheive. The code specifically relevant to this post is in the call to PopulateUnboundComboBox(comboBoxCol, {"Yes", "No"}).
Private Sub GetData()
Const procName As String = "GetData()"
Dim myValue As String = ""
Dim myDataSource As Integer = 0
Try
'First clear the existing grid
With Me.uxGrid
If .ColumnCount > 0 Then
'clear the grid
ClearGrid(Me.uxGrid)
End If
End With
_Logger.SendLog(Me.Name & "." & procName & " - Fetching device data.", NLog.LogLevel.Trace)
'Get the data from the database
If Not _dataSourceID = 0 Then
_sqlStatement = "SELECT ID, TEXT, CATEGORY, MOUNTING, DATASOURCEID, TANALYSIS" & _
" FROM P_TBL_DEVICE WHERE DATASOURCEID = " & _dataSourceID
_criteria = "WHERE DATASOURCEID = " & _dataSourceID
Else
_sqlStatement = ""
_criteria = ""
End If
_myDeviceMngr = New DeviceManager(_currentDB, _userName, _myPwd)
'Now get the latest data
_myDataSet = _myDeviceMngr.GetData(_sqlStatement)
_Logger.SendLog(Me.Name & "." & procName & " - Device data fetch completed.", NLog.LogLevel.Trace)
'Update the display
Call BuildGrid(_myDataSet, uxGrid)
Catch ex As Exception
Beep()
MsgBox(ex.Message, MsgBoxStyle.Exclamation, System.Windows.Forms.Application.ProductName)
_Logger.SendLog(ex.Message & ". Thrown in module " & Me.Name.ToString & "." & procName, NLog.LogLevel.Error, ex)
Finally
Call System.GC.Collect()
End Try
End Sub
Private Sub BuildGrid(ByVal myDataSet As DataSet, ByVal myGrid As DataGridView)
Dim myTable As New System.Data.DataTable
Dim myColCount As Integer
Dim comboBoxCol As DataGridViewComboBoxColumn
Dim readOnlyCellStyle As New DataGridViewCellStyle
Dim textBoxCol As DataGridViewTextBoxColumn
Dim gridObject As Object = Nothing
Const procName As String = "BuildGrid"
readOnlyCellStyle.ForeColor = Color.Gray
Try
_Logger.SendLog(Me.Name & "." & procName & " - Building device data grid.", NLog.LogLevel.Trace, Nothing)
myTable = myDataSet.Tables(0)
With myGrid
'Now add the columns to the grid
Dim column As System.Data.DataColumn
For Each column In myDataSet.Tables(0).Columns
'We dont want to include the changedon and changedby fields in the grid build
If column.ColumnName.IndexOf("CHANGED") = -1 Then
Select Case column.ColumnName
Case "DATASOURCEID"
gridObject = New Object
comboBoxCol = New DataGridViewComboBoxColumn
'First create the comboboxcolumn for the column
'Populate combobox
PopulateScadaSourceComboBoxCol(comboBoxCol)
comboBoxCol.DataPropertyName = "DATASOURCEID"
comboBoxCol.HeaderText = "SCADA SOURCE"
comboBoxCol.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
comboBoxCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
gridObject = comboBoxCol
Case "TEXT"
textBoxCol = New DataGridViewTextBoxColumn
gridObject = textBoxCol
'Now add the columns to the grid
gridObject.DataPropertyName = column.ColumnName.ToString
gridObject.HeaderText = column.ColumnName.Replace("TEXT", "DEVICE")
gridObject.name = column.ColumnName.Replace("TEXT", "DEVICE")
gridObject.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
Case "CATEGORY"
textBoxCol = New DataGridViewTextBoxColumn
gridObject = textBoxCol
'Now add the columns to the grid
gridObject.DataPropertyName = column.ColumnName.ToString
gridObject.HeaderText = "CATEGORY"
gridObject.name = "CATEGORY"
gridObject.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
Case "MOUNTING"
textBoxCol = New DataGridViewTextBoxColumn
gridObject = textBoxCol
'Now add the columns to the grid
gridObject.DataPropertyName = column.ColumnName.ToString
gridObject.HeaderText = "MOUNTING"
gridObject.name = "MOUNTING"
gridObject.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
Case "TANALYSIS"
comboBoxCol = New DataGridViewComboBoxColumn
'First create the comboboxcolumn for the column
'Populate combobox
PopulateUnboundComboBox(comboBoxCol, {"Yes", "No"})
comboBoxCol.DataPropertyName = "TANALYSIS"
comboBoxCol.HeaderText = "TELECONTROL ANALYSIS"
comboBoxCol.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
comboBoxCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
gridObject = comboBoxCol
Case Else
textBoxCol = New DataGridViewTextBoxColumn
gridObject = textBoxCol
'Now add the columns to the grid
gridObject.DataPropertyName = column.ColumnName.ToString
gridObject.HeaderText = "ID"
gridObject.name = "ID"
gridObject.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
End Select
'Now add the textbox columns to the grid
'gridObject.DataPropertyName = column.ColumnName.ToString
'gridObject.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
.Columns.Add(gridObject)
End If
Next
'Set grid default styles/values
.Font = New System.Drawing.Font("Arial", 10, FontStyle.Regular)
.AllowUserToResizeColumns = True
.AllowUserToResizeRows = True
.AllowUserToAddRows = True
.ReadOnly = True
.AutoResizeRows()
.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText
'Now bind the datatable to the DGV datasource property
.DataSource = myTable
End With
_Logger.SendLog(Me.Name & "." & procName & " - Building of device data grid has been completed.", NLog.LogLevel.Trace, Nothing)
Catch ex As Exception
Throw
Finally
textBoxCol = Nothing
comboBoxCol = Nothing
readOnlyCellStyle = Nothing
GC.Collect()
End Try
End Sub
Private Sub PopulateUnboundComboBox(ByVal comboBoxCol As DataGridViewComboBoxColumn, byval valList() as string)
comboBoxCol.Name = "TANALYSIS"
comboBoxCol.DataSource = valList
comboBoxCol.ValueMember = ???
comboBoxCol.DisplayMember = ???
End Sub
EDITED//
Ok, I've made some progress and seem to have almost solved my issue. The one remaining bug is that the column in the DGV is displaying the 'ValueMember' (1 or 0) rather than the 'DisplayMember' (Yes or No). I've checked the valuemember and displaymember properties in the comboboxcolumn definition and and they appear to be set correctly. Here is the associated code:
Excerpt from original code posting listed above
...
Case "TANALYSIS"
gridObject = New Object
comboBoxCol = New DataGridViewComboBoxColumn
'First create the comboboxcolumn for the column
'Populate combobox
Dim item As New CBOItem
Dim itemList As New CBOItemList
item.ValueMember = 0
item.DisplayMember = "No"
itemList.Add(item)
item = New CBOItem
item.ValueMember = 1
item.DisplayMember = "Yes"
itemList.Add(item)
PopulateComboBox(comboBoxCol, itemList)
comboBoxCol.DataPropertyName = "TANALYSIS"
comboBoxCol.HeaderText = "TELECONTROL ANALYSIS"
comboBoxCol.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
comboBoxCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
gridObject = comboBoxCol
...
Private Sub PopulateComboBox(ByRef comboBoxCol As DataGridViewComboBoxColumn, ByVal itemList As CBOItemList)
Dim tbl As DataTable = New DataTable
Dim row As DataRow
tbl.Columns.Add("ValueMember")
tbl.Columns.Add("DisplayMember")
row = tbl.NewRow
row.Item("ValueMember") = itemList(0).ValueMember
row.Item("DisplayMember") = itemList(0).DisplayMember
tbl.Rows.Add(row)
row = tbl.NewRow
row.Item("ValueMember") = itemList(1).ValueMember
row.Item("DisplayMember") = itemList(1).DisplayMember
tbl.Rows.Add(row)
comboBoxCol.ValueMember = tbl.Columns("ValueMember").ToString
comboBoxCol.DisplayMember = tbl.Columns("DisplayMember").ToString
comboBoxCol.DataSource = tbl
End Sub
Kind Regards
Paul J.

how to databind a comboboxcolumn on a datagridview with databind

So i have a datagridview on my project , and this is the code of my datagridview
With DataGridView1
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
.CellBorderStyle = DataGridViewCellBorderStyle.Single
.GridColor = Color.Black
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.MultiSelect = False
.AllowUserToResizeColumns = False
.AllowUserToAddRows = False
.ReadOnly = True
.AutoGenerateColumns = False
Me.DataGridView1.Columns("cod_produto").Visible = True
Me.DataGridView1.Columns("nome_produto").Visible = True
Me.DataGridView1.Columns("descricao").Visible = True
Me.DataGridView1.Columns("qtd_existente").Visible = True
Me.DataGridView1.Columns("qtd_minima").Visible = True
Me.DataGridView1.Columns("precounitario_venda").Visible = True
Me.DataGridView1.Columns("nome_fornecedor").Visible = True
Me.DataGridView1.Columns("categoria_produto").Visible = True
end with
Dim listaproduto As List(Of Produto) = CAD.CAD_Listar.ObterProduto()
Me.BindingSource1.DataSource = listaproduto
DataGridView1.DataSource = BindingSource1
now i wanted to create another column containing a combobox wich i did here
Dim lstcategoria As List(Of CategoriaProduto) = CAD.CAD_Listar.ObterCategoria()
Dim comboboxColumn As New DataGridViewComboBoxColumn
With comboboxColumn
.DataSource = lstcategoria
.DisplayMember = "categoriaproduto"
.ValueMember = "cod_categoriaproduto"
End With
DataGridView1.Columns.Add(comboboxColumn)
but this is not working, it does not trow any exeption but the combobox just stays with no items.i know the list is working because if i create a normal combobox(not in a comboboxcolumn) the combobox fills with the values of the list. but for some reason when i try to do the same but in a comboboxcolumn it just does not fill. can someone help me on this? thanks

Set selectedindex for comboboxcell in datagridview vb.net

I am trying to set the selectedindex for my datagridviewcomboboxcell through cellvaluechanged event,i.e when i change some value in my datagrid row, this column should get automatically changed. here's the code.
Private Sub DataGridView1_CellValueChanged(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
Try
If Me.DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString <> Nothing Then
Me.DataGridView1("unit_code", e.RowIndex).Value = "1"
MsgBox(Me.DataGridView1.Rows(e.RowIndex).Cells(6).Value)
End If
Else
Exit Sub
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
my datagridview is bound to dataset. Searched many sites and did all research, got the above solution where we need to set the valuemember to .value part. Even after doing that it givessystem.formatexception:datagridviewcomboboxcell value is not valid.
Please help me.
[EDIT]
table structure
unit_code descp
0 -
1 nos
2 kgs
3 gms
[EDIT 2]
query = "select Switch(qty=0,'2',qty<=rcvd_qty,'1',rcvd_qty=0,'2',qty>rcvd_qty,'3') as Status,item_type,catalogue,name,pd.rate,qty,pd.unit_code" _
& " from pur_det pd,itemhead th where pd.item_code=th.itemcode and order_no=0 order by catalogue"
adap1 = New OleDbDataAdapter(query, conn)
Dim saldet As New DataSet
adap1.Fill(saldet, "puritems")
Me.DataGridView1.DataSource = saldet.Tables(0)
DataGridView1.Columns.Item(0).HeaderText = "STATUS"
DataGridView1.Columns.Item(0).Width = 68
DataGridView1.Columns.Item(1).HeaderText = "TYPE"
DataGridView1.Columns.Item(1).Width = 60
DataGridView1.Columns.Item(2).HeaderText = "CATALOGUE"
DataGridView1.Columns.Item(2).Width = 140
DataGridView1.Columns.Item(3).HeaderText = "DESCRIPTION"
DataGridView1.Columns.Item(3).Width = 300
DataGridView1.Columns.Item(4).HeaderText = "RATE"
DataGridView1.Columns.Item(4).Width = 60
DataGridView1.Columns.Item(5).HeaderText = "QUANTITY"
DataGridView1.Columns.Item(5).Width = 84
DataGridView1.Columns.Item(6).HeaderText = "UNIT" ' this column is removed below because it only had primary key values of this table("unitmast").
DataGridView1.Columns.Item(6).Width = 70
adap1 = New OleDbDataAdapter("select * from unitmast order by unitcode ASC", conn)
Dim unitc As New DataSet
adap1.Fill(unitc, "unitmast")
Me.DataGridView1.Columns.RemoveAt(6) 'here the same is added with display member to view its actual value
Dim uncol As New DataGridViewComboBoxColumn
With uncol
.Name = "unit_code"
.DataPropertyName = "unit_code"
.DataSource = unitc.Tables(0)
.ValueMember = "unitcode"
.DisplayMember = "desc"
.HeaderText = "UNIT"
.FlatStyle = FlatStyle.Flat
.DropDownWidth = 160
.Width = 70
End With
Me.DataGridView1.Columns.Insert(6, uncol)
A DataGridViewComboBoxCell has no SelectedIndex or SelectedValue property.
You can set the value directly like this:
CType(Me.DataGridView1("unit_code", e.RowIndex), DataGridViewComboBoxCell).Value = "your value string"
Or using the index of the items collection: (this works only if you have not set ValueMember and DisplayMember properties)
Dim combo As DataGridViewComboBoxCell
combo = CType(Me.DataGridView1("unit_code", e.RowIndex), DataGridViewComboBoxCell)
combo.Value = combo.Items('your index')
You can also check a value for nothing like this:
If Not Me.DataGridView1.Rows(e.RowIndex).Cells(2).Value Is Nothing Then
'Your code
End If
I Write and Test that Code For You Good Luck:
Dim Dt As New DataTable
Sub CreateDT()
Dt.Columns.Add("Col1")
Dt.Columns.Add("Col2")
Dt.Rows.Add(New Object(1) {"1", "A"})
Dt.Rows.Add(New Object(1) {"2", "B"})
Dt.Rows.Add(New Object(1) {"3", "C"})
End Sub
Private Sub ChildRibbonForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CreateDT()
'
'Dim CellCol As DataGridViewComboBoxColumn = CType(DataGridView1.Columns(0), DataGridViewComboBoxColumn)
Dim CellCol As New DataGridViewComboBoxColumn
CellCol.Items.Add("Test1")
CellCol.Items.Add("Test2")
CellCol.Items.Add("Test3")
Me.DataGridView1.DataSource = Dt
Me.DataGridView1.Columns.Insert(0, CellCol)
Dim CellBox As DataGridViewComboBoxCell = CType(DataGridView1.Rows(1).Cells(0), DataGridViewComboBoxCell)
'Select the Second Item
CellBox.Value = CellCol.Items(1)
End Sub
'Declare datatable
Dim cdt as new Datatable
'Add columns with types. It is important you specify the type for value member column
cdt.Columns.Add("vm", System.Type.GetType("System.Int32"))
cdt.Columns.Add("dm", System.Type.GetType("System.String"))
'Add items to table
cdt.Rows.Add
cdt.Rows(0).Item(0) = 1
cdt.Rows(0).Item(1) = "Red Cat"
cdt.Rows.Add
cdt.Rows(1).Item(0) = 2
cdt.Rows(1).Item(1) = "Black Cat"
'Add datasource to DataGridViewComboBoxColumn
Dim cc As DataGridViewComboBoxColumn
cc = datagridview1.Columns('colIndex')
cc.DataSource = cdt
cc.ValueMember = "vm"
cc.DisplayMember = "dm"
'Setting then selected value is thus:
Datagridview1.Rows('rowIndex').Cells('colIndex').Value = 2 '2 for black cat
This works for me

Visual basic, fill a combobox

This code gives me system.data.datarowview show in combobox.But I want to fill the combo box with data. whats the error?
Dim adclasstecher As New SqlDataAdapter
Dim tbclassteacher As New DataTable
If cnSchool.State = ConnectionState.Closed Then cnSchool.Open()
cmclassteacher.Connection = cnSchool
cmclassteacher.CommandText = "SELECT * FROM Teachers"
cmclassteacher.ExecuteNonQuery()
adclasstecher.SelectCommand = cmclassteacher
adclasstecher.Fill(tbclassteacher)
With cmbclzteachr
.DataSource = tbclassteacher
.DisplayMember = "ClassTeacher"
.SelectedIndex = 0
End With
cnSchool.Close()
Dim adclasstecher As New SqlDataAdapter
Dim tbclassteacher As New DataTable
If cnSchool.State = ConnectionState.Closed Then cnSchool.Open()
cmclassteacher.Connection = cnSchool
cmclassteacher.CommandText = "SELECT * FROM Teachers"
cmclassteacher.ExecuteNonQuery()
adclasstecher.SelectCommand = cmclassteacher
adclasstecher.Fill(tbclassteacher)
With cmbclzteachr
.DataSource = tbclassteacher
.DisplayMember = "ClassTeacher"
.SelectedIndex = 0
.databind() //you need to bind the data after providing datasource
End With
cnSchool.Close()

How to use BindingNavigator programmatically?

I have a VB.net form and I'm trying to bind some data to controls.
I'm using the following code:
Private Sub InitDataLayer()
'Create table
DataTable = New DataTable
DataTable.Columns.Add("ID")
DataTable.Columns("ID").DataType = GetType(Integer)
DataTable.Columns("ID").AllowDBNull = False
DataTable.Columns.Add("Name")
DataTable.Columns("Name").DataType = GetType(String)
DataTable.Columns("Name").AllowDBNull = False
'Create new rows
'1st Row
Dim NewRow As DataRow = DataTable.NewRow
NewRow.Item("ID") = 1
NewRow.Item("Name") = "John"
DataTable.Rows.Add(NewRow)
'2nd Row
NewRow = DataTable.NewRow
NewRow.Item("ID") = 2
NewRow.Item("Name") = "Steve"
DataTable.Rows.Add(NewRow)
'Bind controls
'Textboxes
TextBoxID.DataBindings.Add(New Binding("text", DataTable, "ID"))
TextBoxName.DataBindings.Add(New Binding("text", DataTable, "Name"))
'Navigator
Dim BS As New BindingSource
BS.DataSource = DataTable
BindingNavigator1.BindingSource = BS
End Sub
The code works but when I move to the second row in the binding navigator, the controls don't update (see Video)
What is missing to the code please ?
Thanks
Solution:
Private Sub InitDataLayer()
'Create table
DataTable = New DataTable
DataTable.Columns.Add("ID")
DataTable.Columns("ID").DataType = GetType(Integer)
DataTable.Columns("ID").AllowDBNull = False
DataTable.Columns.Add("Name")
DataTable.Columns("Name").DataType = GetType(String)
DataTable.Columns("Name").AllowDBNull = False
'Create new rows
Dim NewRow As DataRow = DataTable.NewRow
NewRow.Item("ID") = 1
NewRow.Item("Name") = "John"
DataTable.Rows.Add(NewRow)
NewRow = DataTable.NewRow
NewRow.Item("ID") = 2
NewRow.Item("Name") = "Steve"
DataTable.Rows.Add(NewRow)
'Bind controls
Dim BS As New BindingSource
BS.DataSource = DataTable
TextBoxID.DataBindings.Add(New Binding("text", BS, "ID"))
TextBoxName.DataBindings.Add(New Binding("text", BS, "Name"))
BindingNavigator1.BindingSource = BS
End Sub