Setting DatagridView column type as Combo after databind

Hi,

We’re using a datagridview in our winforms prj ..

Problem is this ..
We have a grid say with 10 columns ..3 columns are combo’s.

ways to go about it are ..

1. set the column heading and column type at design time .. retrieve the records then databind. But in this case .. a new set of columns will be created.
In this scenario, is there a way where I can set the column type of the datagrid view to a datagridviewcombobox column after I do the databind?

2. Have a blank Gridview.Retrieve the records and bind the table to the gridview.
Here again I face the same problem.
In this scenario, is there a way where I can set the column type of the datagrid view to a datagridviewcombobox column after I do the databind?
And if this is possible, can i then bind this combo to another table to get the combo values?

3. what I’m doing now isset the column heading and column type at design time .. retrieve the records then looping thro the rows and columns and then setting the value ..but I don’t think this is the correct way and it looks stupid :)
Since I am also looking at using adapter.update since the grid can contain 1000+ rows easily ..

any ideas ..?

Thanks in advance,
Soni

[2587 byte] By [hangar18] at [2007-12-17]
# 1

What is going on here is that the value of the AutoGenerateColumns property is blowing out the columns that you add.

What you need to do is:
1) Create your DataGridView and add 10 columns to it with 3 being combobox columns
2) For each column set the DataPropertyName property to a string that identifies the column name (or property name) that the column should be bound to when you eventually databind the grid.
3) Right before you perform the databinding (setting the DataGridView's DataSource or DataMember properties) set the AutoGenerateColumns property to false.
4) Now set the DataGridView's DataSource and DataMember properties

You might need to first populate the three combo box columns items collection or databind the columns before you perform step#4. You can do this by just setting the DataGridViewComboBoxColumn DataSource, DataMember, ValueMember and DisplayMember properties OR just add values to the Items collection.

Let me know if this works!
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"

MarkRideout at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
worked like a baby!! :)
I think this must be put up somewhere on MSDN. I tried searching all over but couldn't get the info. Must've been looking at the wrong places I guess.
Thanks a ton!
hangar18 at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

I've just posted a thread about the new DataGridView FAQ (http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=152467&SiteID=1). If you can add a comment to the thread about this scenario it will help me when I go to update the FAQ.

thanks,

-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"

MarkRideout at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4

Hi,

This post is exactly what I needed. I'm having trouble the same way the poster is. I looked at the Doc file you suggested - but it didn't have any info on this (neither did the sample code in the zip files)

I don't know how to get at the combo box (in the datagridview) through code, and fill the items based on another table. "You can do this by just setting the DataGridViewComboBoxColumn DataSource, DataMember, ValueMember and DisplayMember " - How is my question LOL.

Dim cbobox As DataGridViewComboBoxColumn()

cbobox = mygrid.Columns("ColID").?

With cbobox

.DataSource = MyDataTable

.ValueMember = "ColID"

.DisplayMember = "ColName"

End With

There doesn't seem to be any way to set those properties.

I've tried creating the combo prior to adding to the dgv. Which filled the combo with the data great! It updated the cells with the vaule intended. But it didn't update the datatable it was bound to, and when the form loads up, the grid will show all the records ok, EXCEPT for the column which has the combo in it. It shows up as blank.

Pretty frustrating learning new stuff :(

I appreciate any help.

-Denvas

Forgot - also, when I hit ALT+ DownArrow, the combo opens up, I scroll down to the combo item, and hit Enter - and the cell remains empty.

Just had to throw that one in.

Thnx

Denvas at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

You need to cast the return type to a DataGridViewComboBoxColumn. I don't have the VB syntax in front of me, but you use CType function. Something like this I guess:

Dim cbobox as DataGridViewComboBoxColumn()
cbobox = CType(mygrid.Columns("ColID"), GetType(DataGridViewComboBoxColumn))
With cbobox
.DataSource = MyDataTable
.ValueMember = "ColID"
.DisplayMember = "ColName
End With

Look up the CType vb function in the docs for more info (and correct syntax!)

-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"

MarkRideout at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6

Thanks Mark,

I really appreciate the quick response. I never got a response so quick! Actually went out and stopped working b/c I didn't expect it LOL.

Unfortunately, it didn't work. The CType syntax is fine. It's the GetType that I'm having problems with. It returns an error (over the GetType) saying "Keyword does not name a type". I tried just for giggles, to declare a variable as a type, then set it to cbobox.GetType(). And that didn't work either.

Man, there has to be an easier way. LOL I don't mind C# syntax if you have it. I can run it through a translator if I have to! :)

Thanks again for all the help. It's VERY appreciated.

Denvas

Denvas at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...