ComboBox Column in a Bound DataGridView


T

TC

How do I configure a bound DataGridView to use ComboBoxes in some
columns instead of TextBoxes?

I'm using Visual Studio 2005. I have a DataGridView which is bound to
a DataTable. When I bind the grid to the table, I automatically get a
column of TextBoxes for each column in the table. I'd like some of
those columns to be columns of ComboBoxes, not columns of TextBoxes.
Unfortunately, I can't figure out how to make that happen.

I've researched the DataGridViewComboBoxColumn and
DataGridViewComboBoxCell classes, and I have no trouble creating a
column of ComboBoxes and populating the ComboBox selection list. I
understand how to add a column of ComboBoxes to a grid and I
understand how to change the CellTemplate for unbound columns.
However, I don't understand how to make a bound column use ComboBoxes
instead of TextBoxes. Can anyone point me in the right direction?

-TC
 
Ad

Advertisements

T

TC

How do I configure a bound DataGridView to use ComboBoxes in some
columns instead of TextBoxes?

I'm using Visual Studio 2005. I have a DataGridView which is bound to
a DataTable. When I bind the grid to the table, I automatically get a
column of TextBoxes for each column in the table. I'd like some of
those columns to be columns of ComboBoxes, not columns of TextBoxes.
Unfortunately, I can't figure out how to make that happen.

I've researched the DataGridViewComboBoxColumn and
DataGridViewComboBoxCell classes, and I have no trouble creating a
column of ComboBoxes and populating the ComboBox selection list. I
understand how to add a column of ComboBoxes to a grid and I
understand how to change the CellTemplate for unbound columns.
However, I don't understand how to make a bound column use ComboBoxes
instead of TextBoxes. Can anyone point me in the right direction?

-TC

I've done some more research on this and I discovered the
DataGridView's AutoGenerateColumns property. My original question, in
essence, was about how to use ComboBox columns in a DataGridView with
AutoGenerateColumns = True. I now believe you can't do that; to get
ComboBox columns in a bound DataGridView, you have to set
AutoGenerateColumn to False and build each column manually. I'm
figuring this out through trial and error. If any expert out there has
some authoritative comments on this subject, I'd be grateful for your
input.

-TC
 
Ad

Advertisements

J

Jarek Mielcarek

W dniu 2009-12-27 23:42, TC pisze:
How do I configure a bound DataGridView to use ComboBoxes in some
columns instead of TextBoxes?
However, I don't understand how to make a bound column use ComboBoxes
instead of TextBoxes. Can anyone point me in the right direction?

This function create combobox column you can add to datagridview.
Take a look to datapropertyname, displaymember and valuemember.

Private Function CreateInOutCombo() As DataGridViewComboBoxColumn
Dim col As New DataGridViewComboBoxColumn

With col
.DataPropertyName = "someColumn"
.DataSource = myDataTable
.DisplayMember = "column1"
.ValueMember = "column2"
.HeaderText = "We/Wy"
.Name = "someColumn"
End With
Return col
End Function

Jarek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top