Solved

Adding A Combo-Box to a DataGridView

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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