Add Combo Box Selected value to Data Table

  • Thread starter Thread starter Shan1986
  • Start date Start date
S

Shan1986

Guest
Hallo

I have 3 cascading combo boxes and try to add their SelectedValue to my Datatable by button click. I can add a new row to my datatable but the combobox selected values are not updated/ added to my datatable. If i add a second row i loose my values set in the firstrow. Could you please tell me what is the correct procedure to set the combobox selected values to datatable? and also move the bindingsource position to newly added row.


Create dataset


Public Data_Set As DataSet = New DataSet("Project")
Public tbl_Equipment As DataTable = New DataTable("Equipment")

Sub Create_DS()

With DataSet

.Tables.Add(tbl_Equipment)
With tbl_Equipment
.Columns.Add(dtID, GetType(String))
.Columns.Add(dtName, GetType(String))

.Columns.Add(dtEQID, Type.GetType("System.Int32"))
.Columns(dtEQID).AutoIncrement = True
.Columns(dtEQID).AutoIncrementSeed = 1
.Columns(dtEQID).AutoIncrementStep = 1
.PrimaryKey = New DataColumn() { .Columns(dtEQID)}

.Columns.Add(New DataColumn(dtEqp, Type.GetType("System.String")))
.Columns.Add(New DataColumn(dtTyp, Type.GetType("System.String")))
.Columns.Add(New DataColumn(dtMat, Type.GetType("System.String")))


End with
End with

End sub


Add Databindings


Public EQ_BS As New BindingSource

sub data_bindings()

EQ_BS.DataSource = tbl_Equipment

With form1.VT_lbx_eqlist
.ValueMember = dtEQID
.DisplayMember = dtName
.DataSource = EQ_BS
End With

With form1
.vt_cbx_eq.DataBindings.Add("SelectedValue", EQ_BS, dtEqp, True, DataSourceUpdateMode.OnPropertyChanged)
.vt_cbx_mat.DataBindings.Add("SelectedValue", EQ_BS, dtMat, True, DataSourceUpdateMode.OnPropertyChanged)
.vt_cbx_type.DataBindings.Add("SelectedValue", EQ_BS, dtTyp, True, DataSourceUpdateMode.OnPropertyChanged)
End with
AddHandler EQ_BS.BindingComplete, AddressOf BindingComplete

End sub

Sub BindingComplete(ByVal sender As Object, ByVal e As BindingCompleteEventArgs)

If e.BindingCompleteContext = BindingCompleteContext.DataSourceUpdate AndAlso e.Exception Is Nothing Then e.Binding.BindingManagerBase.EndCurrentEdit()

End Sub


Add new row by button click

Sub Add_Equip_DT(Optional ByVal pos As Integer = vbEmpty)

EQ_BS.EndEdit()

Dim tmpEQP As String
Dim tmpTyP As String
Dim tmpMAT As String

With Form1
.vt_cbx_eq.SelectedIndex = 0
tmpEQP = .vt_cbx_eq.SelectedValue

.vt_cbx_type.SelectedIndex = 0
tmpTyP = .vt_cbx_type.SelectedValue

.vt_cbx_mat.SelectedIndex = 0
tmpMAT = .vt_cbx_mat.SelectedValue

End With

Dim nRow = DirectCast(EQ_BS.AddNew(), DataRowView)
nRow(dtEquipment) = tmpEQP
nRow(dtType) = tmpTyP
nRow(dtMaterial) = tmpMAT
nRow(dtName) = "New Equipment"
EQ_BS.EndEdit()

'//Set the BS to newly adeed row position
With Form1
EQ_BS.Position = EQ_BS.Find("ID", nRow.Row.ItemArray(1))
End With



Thanks

Continue reading...
 
Back
Top