?
Solved

Adding A Combo-Box to a DataGridView

Posted on 2012-04-04
5
Medium Priority
?
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
nepaluz earned 2000 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 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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 brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 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