Link to home
Start Free TrialLog in
Avatar of alfardan
alfardanFlag for United Arab Emirates

asked on

Adding A Combo-Box to a DataGridView

Hi again

Reference to my previous question (https://www.experts-exchange.com/questions/27661434/Displaying-an-Image-in-a-DataGridView-Cell.html)

Now, what I'm looking for is to add a combo-box field in each row instead of an image, and for each combo-box in each row will have three selections ("Yes", "No", "I don't Know")

However, the selected value of each combo-box in each row could differ from the other combo-box selected values of the other rows, so some combo-boxes in some rows will have ("Yes") selected, some will have ("No") selected and the rest will have ("I don't know") selected depending on some criteria that I determine in my application.

I tried following the instructions in (http://vb.net-informations.com/datagridview/vb.net_datagridview_combobox.htm) but it generated an error for me (see attachment)

This is my full code for the grid generation:

            Dim ApplicationList As New DataSet
            ApplicationList = MC.DB(AppListSQLStr(MC.UserSysID()))

            Dim img_down As New DataGridViewImageColumn()
            Dim inImg1 As Image = Image.FromFile(IO.Directory.GetCurrentDirectory() + "\AppImages\down_arrow.jpg")
            img_down.Name = "MoveDown"
            img_down.HeaderText = ""
            img_down.Image = inImg1

            Dim img_up As New DataGridViewImageColumn()
            Dim inImg2 As Image = Image.FromFile(IO.Directory.GetCurrentDirectory() + "\AppImages\up_arrow.jpg")
            img_up.Name = "MoveUp"
            img_up.HeaderText = ""
            img_up.Image = inImg2

            '================================================
            'HERE IS THE COMBOBOX CODE I WANTED TO ADD
            Dim cmb As New DataGridViewComboBoxColumn()
            cmb.HeaderText = "Is Primary App?"
            cmb.Name = "cmb"
            cmb.MaxDropDownItems = 3
            cmb.Items.Add("Yes")
            cmb.Items.Add("No")
            cmb.Items.Add("I Don't Know")
            '================================================


            AppListOrderGridView.Columns.Add(img_down)
            AppListOrderGridView.Columns.Add(img_up)
            AppListOrderGridView.Columns.Add("ApplNum", "No.")
            '================================================
            'HERE IS HOW I WANTED TO ADD IT TO MY DATAGRID
            AppListOrderGridView.Columns.Add(cmb)
            '================================================
            AppListOrderGridView.Columns.Add("ApplName", "Application Name")
            AppListOrderGridView.Columns.Add("OrderID", "Order ID")
            AppListOrderGridView.Columns.Add("ApplOrder", "Order Value")


            For i = 0 To ApplicationList.Tables(0).Rows.Count - 1
                '================================================
                'I ADDED THE VARIABLE (CMB) HERE TOO AS A 3RD COLUMN IN THE TO-BE-ADDED ROW
                AppListOrderGridView.Rows.Add(inImg1, inImg2, (i + 1), cmb, ApplicationList.Tables(0).Rows(i).Item("Appname"), ApplicationList.Tables(0).Rows(i).Item("ID"), ApplicationList.Tables(0).Rows(i).Item("AppOrder"))
                '================================================
            Next


            For i = 0 To AppListOrderGridView.ColumnCount - 1
                AppListOrderGridView.Columns(i).AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells
                AppListOrderGridView.Columns(i).SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable
            Next

            AppListOrderGridView.Columns("OrderID").Visible = False
            AppListOrderGridView.Columns("ApplOrder").Visible = False


            AppListOrderGridView.Columns("ApplName").AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill

Open in new window


Just see where my code comments are and you'll see how I tried to add this combo-box control in my grid.

Any suggestions?
ErrorMessage.png
ASKER CERTIFIED SOLUTION
Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alfardan

ASKER

Done so, same error

I believe the error is within this code:

            For i = 0 To ApplicationList.Tables(0).Rows.Count - 1
                AppListOrderGridView.Rows.Add(inImg1, inImg2, (i + 1), cmb, ApplicationList.Tables(0).Rows(i).Item("Appname"), ApplicationList.Tables(0).Rows(i).Item("ID"), ApplicationList.Tables(0).Rows(i).Item("AppOrder"))
            Next

Open in new window


it's when I add it to each row, I add it as a "cmb", that's the issue I believe, and it's wrong to pass it as a "cmb" then how to pass it then? Or is this row addition operation wrong by itself or ... ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great, now I don't have the error, but now at run time, whenever I see this grid then go to the combo-box column to try to change the "Yes" by trying to select another value (either "No" or "I don't Know"), the combo-box doesn't allow me too, it does not show me a drop list of these other selection options :(
Nevermind, I got it fixed now, I just let that combo-box column to be the only one with property "ReadOnly=False" while the whole grid to be "ReadOnly=True"