Combo Box in DataGridView

partham

Active member
Joined
Dec 27, 2005
Messages
41
Location
Kolkata, India
Programming Experience
10+
Dear Sir/Madam,

We are now using VB.Net 2008.

We have used a DataGridView. The DataSource for the DataGridView is a DataTable.

We have created the DataTable as per the Database table in ORACLE. Suppose, we have the following columns in the DataTable:

A - string
B - string
C - boolean

When the DataGridView appears, it makes columns A and B TextBoxes and C a checkboxcolumn.

However, we want the column B to be a dropdown list.

When we create a DataGridViewComboxCell object and assign the values to display, etc and then try to change the CellTemplate of the DataGridViewColumn (B), it gives a run time error stating that type cast is not possible.

Our request is to kindly let us know how we can use a DataTable as the DataSource of a DataGridView and make one or more of the columns as a ComboBox.



Our code logic is as follows:

' Create the Data Table
' Populate the Data Table fetching data from the Database
' Attach the DataTable as the DataSource of the DataGridView
dgView.DataSource = _dtblModel
' Then create the ComboBox cell
cell = New DataGridViewComboBoxCell
cell.DataSource = getDataFromDB
cell.DisplayMember = "Description"
cell.ValueMember = "Key"
dgView.Column("B").CellTemplate = cell
'<--- Here is an error



We would like to know the correct technique as we are used to DataGrid and not experienced with DataGridView.

Please help us.

Regards,
Partha
 
You could take a look at this thread from MSDN. It contains a step-by-step example of how to do what you want, with pictures (on another site unfortunately, hope you have tabs on your browser :) )

It also has a link to another supposed solution, which I haven't looked at.

I have just found this video on MSDN How do I - Lookup Lists which is probably better than the above references.

What you are trying to do is use a Lookup ComboBox so if these suggestions are no good try Googling/Binging for that.

Good Luck! :)
 
Back
Top