S
Shan1986
Guest
Hi,
I am trying to add a new row to a binding source from a combo box selection (text and selected value) below is my code but it is not working as expected. i a getting error on the following line
DirectCast(.Current, DataRowView)("sSize") = ComboBox1.SelectedValue
my code
Public Class Form1
Dim dt_cbx_Source As New DataTable("Table1")
Dim dt_Save As New DataTable("Save")
Dim bs_Source As New BindingSource
Dim bs_Save As New BindingSource
Dim testPath As String = "Table1.xml"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'//populate source datatable from xml file
With dt_cbx_Source
.Columns.Add("Product", GetType(String))
.Columns.Add("Size", GetType(Single))
.ReadXml(testPath)
End With
'//create datatble to save selected items
With dt_Save
.Columns.Add("sProduct", GetType(String))
.Columns.Add("sSize", GetType(Single))
End With
'//bind cb source data to dgv ..just to check values
bs_Source.DataSource = dt_cbx_Source
With DataGridView1
.DataSource = bs_Source
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
End With
'//populate combobox from source datatable
With ComboBox1
.DisplayMember = "Product"
.ValueMember = "Size"
.DataSource = bs_Source
End With
'//bind save dt to dgv2
bs_Save.DataSource = dt_Save
With DataGridView2
.DataSource = bs_Save
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
End With
ComboBox1.DataBindings.Add("Text", bs_Save, "sProduct", True, DataSourceUpdateMode.OnPropertyChanged)
ComboBox1.DataBindings.Add("SelectedValue", bs_Save, "sSize", True, DataSourceUpdateMode.OnPropertyChanged)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
With bs_Save
.AddNew()
DirectCast(.Current, DataRowView)("sProduct") = ComboBox1.Text
DirectCast(.Current, DataRowView)("sSize") = ComboBox1.SelectedValue
.EndEdit()
End With
End Sub
End Class
What is the right way to add a new row to a binding source?
Thanks
Continue reading...
I am trying to add a new row to a binding source from a combo box selection (text and selected value) below is my code but it is not working as expected. i a getting error on the following line
DirectCast(.Current, DataRowView)("sSize") = ComboBox1.SelectedValue
my code
Public Class Form1
Dim dt_cbx_Source As New DataTable("Table1")
Dim dt_Save As New DataTable("Save")
Dim bs_Source As New BindingSource
Dim bs_Save As New BindingSource
Dim testPath As String = "Table1.xml"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'//populate source datatable from xml file
With dt_cbx_Source
.Columns.Add("Product", GetType(String))
.Columns.Add("Size", GetType(Single))
.ReadXml(testPath)
End With
'//create datatble to save selected items
With dt_Save
.Columns.Add("sProduct", GetType(String))
.Columns.Add("sSize", GetType(Single))
End With
'//bind cb source data to dgv ..just to check values
bs_Source.DataSource = dt_cbx_Source
With DataGridView1
.DataSource = bs_Source
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
End With
'//populate combobox from source datatable
With ComboBox1
.DisplayMember = "Product"
.ValueMember = "Size"
.DataSource = bs_Source
End With
'//bind save dt to dgv2
bs_Save.DataSource = dt_Save
With DataGridView2
.DataSource = bs_Save
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
End With
ComboBox1.DataBindings.Add("Text", bs_Save, "sProduct", True, DataSourceUpdateMode.OnPropertyChanged)
ComboBox1.DataBindings.Add("SelectedValue", bs_Save, "sSize", True, DataSourceUpdateMode.OnPropertyChanged)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
With bs_Save
.AddNew()
DirectCast(.Current, DataRowView)("sProduct") = ComboBox1.Text
DirectCast(.Current, DataRowView)("sSize") = ComboBox1.SelectedValue
.EndEdit()
End With
End Sub
End Class
What is the right way to add a new row to a binding source?
Thanks
Continue reading...