DataGridViewComboBoxColumn Default value and Item Collection

retkehing

Well-known member
Joined
Feb 5, 2006
Messages
153
Programming Experience
Beginner
I have been researching the DataGridView for few days but the progress is not that up to my expectation and hence i hope to get help from you guys. I have already inserted a ComboBoxColumn in my DataGridView, i want my combobox to retrieve the default data from the database for that particular row, the data can be "Pending, Approved, Disapproved".

I managed to do this by defining the .DataSource, .DataPropertyName but i can't insert "Pending, Approved, Disapproved" as an item collection for combobox.

If i define the item of combox using Items.Add then i can't make use of the .DataPropertyName anymore and the combobox default value will be null. It seems no matter how hard i try, i can only fulfill one of my design requirements. Anyone can help on it? Thank you.


VB.NET:
            Dim DGVColumn As New DataGridViewComboBoxColumn
            With DGVColumn
 
                .HeaderText = "Plan Status"
 
                .DataSource = DataSet2.Tables("Plan_Status")
                .DataPropertyName = "p_status"
                .ValueMember = "p_status"
                .DisplayMember = "combo1"
 
                .Width = "200"
 
            End With
            DataGridView1.Columns.Add(DGVColumn)
 
Last edited:
Like any other combobox, you need to have 2 tables, one to write the selected value into (the main data table) and one lookup table. Hangon while i find an example from my code
 
It would look something more like this:


VB.NET:
            Dim DGVColumn As New DataGridViewComboBoxColumn
            With DGVColumn
 
                .HeaderText = "Plan Status"
 
                .DataPropertyName = "[I]p_status_in_Plan_Status_Table[/I]"
                
                .DataSource = DataSet2.Tables("[B]Lookup_Table[/B]")
                .ValueMember = "[B]p_status_in_Lookup_Table[/B]"
                .DisplayMember = "[B]p_display_in_Lookup_Table[/B]"
 
                .Width = "200"
 
            End With
            DataGridView1.Columns.Add(DGVColumn)
 
            DataGridView1.DataSource = DataSet2.Tables("[I]Plan_Status_Table[/I]")

Now make sure your DataSet2 has a table called Lookup_Table
And this table has 2 columns of names like the bold above.

column p_status_in_Lookup_Table has entries like: 1, 2, 3, etc
column p_display_in_Lookup_Table has entires like: Active, Finished, Deferred, etc

column p_status_in_Plan_Status_Table has data like: 1,3,1,1,2,3,1,3,3,3,2
 
Back
Top