Solved

Adding A Combo-Box to a DataGridView

Posted on 2012-04-04
5
402 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now