I want to store C and D from selected combobox item which is manually feed data "C|Credit, D|Debit". Followint is my code for that
FillCombosWithValueMDisplayM(m01OpnCrDr, "C|Credit,D|Debit")
'Calling in form_load
but when I collect data from various control on Form then that time combobox returns 'Nothing'.
I tried the following coding
Following sub to fill manually data to combo is in common module:
Sub FillCombosWithValueMDisplayM(CtrlCombo As ComboBox, CommaSepVDPair As String)
Dim pairs() = CommaSepVDPair.Split(",")
For Each pair As String In pairs
Dim KeyVal() = pair.Split("|")
Dim newItem As New With {.Value = KeyVal(0).ToString, .Display = KeyVal(1).ToString}
CtrlCombo.Items.Add(newItem)
Next
CtrlCombo.ValueMember = "Value"
CtrlCombo.DisplayMember = "Display"
End Sub
Now there is other common function that Collects the data from various control on form (winform). There I check Control type and datatype of field refine the data and add to keyvalue pair.
So, the Common CollectFormData Function is as below (partial). Where I detect m01OpnCrDr combobox and try to get value C (if credit is selected) or D (if Debit is selected) to store that C or D to database field. But Combobox return 'Nothing'.
Function to Collect Form Data:
Function CollectFormData(frm As Form, tbl As DataTable)
'Function CollectFormData(frm As Form, FieldList As List(Of String)) As Dictionary(Of String, Object)
Dim FormDataDict As New Dictionary(Of String, Object)
Dim CtlType As Type
Dim CtrlArray As Control()
Dim Ctrl As Control
Dim fldname As String
Dim FldType As OleDb.OleDbType
Dim CtlValuePlaceHolder As Object
For Each row As DataRow In tbl.Rows
fldname = row("COLUMN_NAME")
FldType = row("DATA_TYPE").ToString
If fldname = "ID" Then
'do nothing
Else
CtrlArray = frm.Controls.Find(fldname, True)
If CtrlArray.Count > 0 Then
Ctrl = CtrlArray(0)
CtlType = Ctrl.GetType
Select Case CtlType
Case GetType(TextBox)
CtlValuePlaceHolder = CNulls(TryCast(Ctrl, TextBox).Text, FldType)
FormDataDict.Add(fldname, CtlValuePlaceHolder)
Case GetType(ComboBox) **'(here combobox m01OpnCrDr returns Nothing)**
'CtlValuePlaceHolder = CNulls(TryCast(Ctrl, ComboBox).SelectedValue, FldType)
Dim selectedValue As Object = TryCast(Ctrl, ComboBox).SelectedValue
CtlValuePlaceHolder = CNulls(selectedValue, FldType)
FormDataDict.Add(fldname, CtlValuePlaceHolder)
'CtlValuePlaceHolder = CNulls(selectedValue, FldType)
'FormDataDict.Add(fldname, CtlValuePlaceHolder)
Case GetType(C1.Win.Calendar.C1DateEdit)
CtlValuePlaceHolder = CNulls(TryCast(Ctrl, C1.Win.Calendar.C1DateEdit).Value, FldType)
FormDataDict.Add(fldname, CtlValuePlaceHolder)
Case GetType(CheckBox)
If TryCast(Ctrl, CheckBox).Checked Then
CtlValuePlaceHolder = True
Else
CtlValuePlaceHolder = False
End If
FormDataDict.Add(fldname, CtlValuePlaceHolder)
Case GetType(DateTimePicker)
CtlValuePlaceHolder = CNulls(TryCast(Ctrl, DateTimePicker).Value, FldType)
FormDataDict.Add(fldname, CtlValuePlaceHolder)
Case GetType(C1.Win.Calendar.C1DateEdit)
CtlValuePlaceHolder = CNulls(TryCast(Ctrl, C1.Win.Calendar.C1DateEdit).Value, FldType)
FormDataDict.Add(fldname, CtlValuePlaceHolder)
Case GetType(MaskedTextBox)
CtlValuePlaceHolder = CNulls(TryCast(Ctrl, MaskedTextBox).Text, FldType)
FormDataDict.Add(fldname, CtlValuePlaceHolder)
End Select
End If
End If
Next
Return FormDataDict
End Function
So, finally I got the solution. Now I am not filling combobox with manual data feed. Instead now I use DATATABLE. I gave data table as source to combobox and it works. In manually adding items to combobox with property (value and display), property does not set properly and it does not treated as row.
so the best way is to create data table and source it to combobox. My changed code is as below.
Then fix sub to fill CRDRcombo whenever required.
Then in data collection from form it works fine. it gets value (either C or D) following is the code:
My Problem solved