Solved

Adding A Combo-Box to a DataGridView

Posted on 2012-04-04
5
411 Views
Last Modified: 2012-04-05
Hi again

Reference to my previous question (http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_27661434.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
0
Comment
Question by:alfardan
  • 3
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 37809930
Try defining the comboboxcolumn like this:
        Dim cmb As New DataGridViewComboBoxColumn()
        cmb.HeaderText = "Is Primary App?"
        cmb.Name = "cmb"
        cmb.MaxDropDownItems = 3
        cmb.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
        cmb.DataSource = {"Yes", "No", "I Don't Know"}

Open in new window

0
 

Author Comment

by:alfardan
ID: 37810147
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 ... ?
0
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 500 total points
ID: 37810194
What are thevalues you want to set on a new row? You can not add a column to a row.
1. Add the columns to the datagrid
2. When adding a new row, simply use the Add method with the item value array (not the column since you have already added these in step 1)
AppListOrderGridView.Rows.Add(inImg1, inImg2, (i + 1), "Yes", ApplicationList.Tables(0).Rows(i).Item("Appname"), ApplicationList.Tables(0).Rows(i).Item("ID"), ApplicationList.Tables(0).Rows(i).Item("AppOrder"))

Open in new window

For example, I have used the value "Yes" above (you can use Nothing instead)
0
 

Author Comment

by:alfardan
ID: 37810224
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 :(
0
 

Author Comment

by:alfardan
ID: 37810238
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"
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question