DataGridView comboboxes

rpiller

Member
Joined
Feb 6, 2007
Messages
8
Programming Experience
5-10
I have a datagridview control and it has 3 combo boxes on it. I wish the datagridview to populate from a query. My issue is that these combo boxes have logic behind them. When you make a selection on the first it determines what data shows up in the second. When make a selection on the second it determines what shows up in the third. So when the form is first shown I just populate the valid values for the first combobox. This all works fine UNTIL I wish to populate the datagrid from the select query.

Since the second and third comboboxes aren't populated right away because they have dependecies, the population of the datagridview from the database query errors out because it's expecting the second and third comboboxes to have values to select from.

It's like when I do my initial load I just want the data from the database to be added to the control no matter what. But when adding records the logic of these comboboxes is used.

Any ideas how I can do this?
 
You could use the datasource property of the column where the comboboxes reside and assign a displaymember to it (= name of the datasource/table/set). But mind that this is
a general solution and is harder to work with.
What I did is the following: (mind that this is just a snippet from code I made a loooong time ago :))

VB.NET:
Dim ds_rc As DataSet = ASW.Open("SELECT * FROM ALI452BFAL.Z2OBCRC", "RC")

        If ds.Tables(0).Rows.Count > 0 Then

            For Each r In ds.Tables(0).Rows

                Dim cbc As DataGridViewComboBoxCell

                dataToevoegen_Orderlijnen.Rows.Add()

                dataToevoegen_Orderlijnen.Item(0, dataToevoegen_Orderlijnen.Rows.Count - 1).Value = False
                dataToevoegen_Orderlijnen.Item(1, dataToevoegen_Orderlijnen.Rows.Count - 1).Value = VAR.sDOC
                dataToevoegen_Orderlijnen.Item(2, dataToevoegen_Orderlijnen.Rows.Count - 1).Value = r.Item("ITLINE")
                dataToevoegen_Orderlijnen.Item(3, dataToevoegen_Orderlijnen.Rows.Count - 1).Value = r.Item("ITPRDC")
                dataToevoegen_Orderlijnen.Item(4, dataToevoegen_Orderlijnen.Rows.Count - 1).Value = r.Item("ITKL1")
                dataToevoegen_Orderlijnen.Item(5, dataToevoegen_Orderlijnen.Rows.Count - 1).Value = r.Item("ITKL2")
                dataToevoegen_Orderlijnen.Item(6, dataToevoegen_Orderlijnen.Rows.Count - 1).Value = r.Item("ITLENG")
                dataToevoegen_Orderlijnen.Item(7, dataToevoegen_Orderlijnen.Rows.Count - 1).Value = " "
                dataToevoegen_Orderlijnen.Item(8, dataToevoegen_Orderlijnen.Rows.Count - 1).Value = r.Item("ITCQTS")
                'dataToevoegen_Orderlijnen.Item(10, dataToevoegen_Orderlijnen.Rows.Count - 1).Value = r.Item("CREDIT")

                cbc = dataToevoegen_Orderlijnen.Item(11, dataToevoegen_Orderlijnen.Rows.Count - 1)

                For Each ri As DataRow In ds_rc.Tables(0).Rows
                    cbc.Items.Add(ri.Item("CLDESC"))
                Next

Basically you create a dataset with your query, then iterate through the rows
and add a datagridviewcomboboxcell to the row.
 
Back
Top