fill DataGridView combobox

sachmo

Member
Joined
Aug 2, 2007
Messages
14
Programming Experience
Beginner
Please someone show me how to add text to a Datagridview row in code, not using a database, but defined string text. All of my cells in the first column have the combo box thing set and I want to fill each one of those combo boxes with three different text values, lets say:

. Good
. Bad
. Ugly

TIA
 
Hey John, I tried that but it tells me this:

Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxColumn' to type 'System.Windows.Forms.DataGridViewComboBoxColumn'.

my code is this:

Dim aa As DataGridViewComboBoxColumn
aa = DataGridView1.Columns.Item(1)
aa.Items.AddRange("Good", "Bad", "Ugly")

TIA
 
Try the first column (0).
 
Hey John, I changed my code to this:

VB.NET:
        Dim aa As DataGridViewComboBoxColumn
        aa = DataGridView1.Columns.Item(0)
        aa.Items.AddRange("Good", "Bad", "Ugly")
        aa.DefaultCellStyle.NullValue = "Good"

And it compiled and ran fine, but the combo boxes did not have any data in them as expected. The way my form is setup is like this:

Header column (the one with the arrow denoting which row your on), then the next column over is where my combo boxes are. So it should be column 1, item 1, correct? Maybe that is why it isn't working to show my data. [EDIT] John, I also added the last line to my code and it shows the value Good, but I can't see the rest of the entries that should be there. And I checked the column number and it should be Column1.

Any help?

TIA
 
Last edited:
Wat u do is,
1. Create Object For Datagridviewcomboboxcolumn
2. And Items To that Object (Just LIke Normal Combobox)
3. Finally Add That Object To Datagridview( Using Columns.Add() method )
This Will Certainly Help , Bye
 
Hey John, I changed my code to this:

VB.NET:
        Dim aa As DataGridViewComboBoxColumn
        aa = DataGridView1.Columns.Item(0)
        aa.Items.AddRange("Good", "Bad", "Ugly")
        aa.DefaultCellStyle.NullValue = "Good"

And it compiled and ran fine, but the combo boxes did not have any data in them as expected. The way my form is setup is like this:

Header column (the one with the arrow denoting which row your on), then the next column over is where my combo boxes are. So it should be column 1, item 1, correct? Maybe that is why it isn't working to show my data. [EDIT] John, I also added the last line to my code and it shows the value Good, but I can't see the rest of the entries that should be there. And I checked the column number and it should be Column1.

Any help?

TIA
That code works fine here. Column(0) is your first column. You can also edit the column in Designer and add the Items there and set the DefaultCellStyle.
 
prabumj, sorry, it doesn't help unless I see code, I have never worked with the .net language before, and it isn't very well documented. How anyone knows how to use the vb.net compiler is beyond me. Anyway, I tried doing what you said but it doesn't work for me.

Can you write me up a sample code and I will try it out and hopefully it will work so I can move on to actually writing my code vs. setting up the gui.

TIA

[EDIT]

John, I will try your suggestions. thanks
 
The .Net Framework is very well documented, have you tried Help (press F1), also much more available at MSDN.
 
John, I tried doing that in the designer, I set the Items Collection with my three values, entered a NullValue and when I run the app, I still can't get the drop down box to display the two other values I set. I checked the settings I don't see where it could be disabled or anything. Do I need to set something to allow the combo box to drop down?

TIA
 
Not that I know of.
 
John, this is killing me. How could something so simple become something so hard? Can you wrap me up a sample project and attach it for me? That way I can look at your settings and check it with mine. I would really appreciate it.

TIA


[EDIT]

John, nix the sample bro. I figured it out. It does have a property that determines whether it is shown or not and it is called Allow Editing, which I had disabled. thanks
 
Last edited:
There you go, can't edit a value when editing is not allowed. Didn't think of that. You can set ReadOnly for individual columns that you don't want user to edit.
 
Hi,

Dim col_Combo As New DataGridViewComboBoxColumn
With DataGridView1

' MethodUsing Dataset To Add Items In Combobox Column
With col_Combo
.HeaderText = "ID"
.Width = 80
.DataSource = Dataset.Tables(0) ' i m using dataset to list item in combobox

.DisplayMember = "ID" ' ID is One Of The Column In Dataset
.ValueMember = "ID"
.DisplayStyleForCurrentCellOnly = True ' Only After U Click / Select The -
' - Cell Combobox Will Be Visible
End With
' Second Method Which is just like normal combobox
With col_Combo
.items.add("Good")
.items.add("bad")
.items.add("Ugly")
end with

.columns.add(col_Combo)

END WITH ' this "end with " is to end DataGridView1 (for wat we start very first)"

Use Either First Method Or Second Method ,BYe
 
Back
Top