ComboBoxes in DataGridViews bound to DataSources

J

jjsavage

Hi all,
I've got a DataGridView bound to a DataTable, and I need to make
some of the columns into ComboBoxColumns. I've tried some of the
solutions listed here, but I can't seem to make them work. They might
not be the solutions I'm looking for; I just want to have a few
arbitrary values for a combo box and make sure everything in that row
is one of those values. My boss won't let me just leave them as text
boxes. How do I tell the DataTable to tell the DataGridView to display
a ComboBox?

Thanks,
John
 
J

jjsavage

It needs to be automatic; it takes 45 seconds to load 5,000 rows
without binding, but only 6 seconds with binding. Somehow, I need to
have the DataSource create a ComboBox column automatically.

- John
 
R

RobinS

In Brian Noyes' databinding book, he has one example where
he creates a DataGridView, and has different kinds of columns
in it. One of them is a dropdown combo box. He says this is
one of the most complicated to implement.

I recommend that you go to his website
www.softinsight.com/databindingbook
and download the code and look at the project in Chapter6
called ColumnTypes.

I'd post some of it here, but there's a lot of it,
tied to different events (like editing the grid).

If you want to run it, you will need to set up the database
he's using; those scripts are on the same webpage.

Then you should buy this book; it's invaluable.

Robin S.
---------------------------

It needs to be automatic; it takes 45 seconds to load 5,000 rows
without binding, but only 6 seconds with binding. Somehow, I need to
have the DataSource create a ComboBox column automatically.

- John
 
J

jjsavage

Ah ha! Thanks, that had what I needed. In case anyone else is
wondering, here's what you do:
1. Create a DataGridView with the columns and column types (text,
combobox, checkbox, etc) that you want.
2. Set DataGridView.AutoGenerateColumns to false.
3. Create and fill a DataTable with your data.
4. The important (and non-obvious) part: each existing column in the
DataGridView must be individually connected to its corresponding column
in the DataTable:
DataGridView.Columns[0].DataPropertyName = "Column0";
DataGridView.Columns[1].DataPropertyName = "Column1";
...
5. Set DataGridView.DataSource to the DataTable from 3.

You would think that if AutoGenerateColumns is false, the grid would
automatically connect its columns to the columns of its DataSource, but
I guess it would be too hard to figure out if it's supposed to match
them by position or name or something else, so they left it up to the
programmer.

- John
 
R

RobinS

But don't you also have to connect the column of the combobox
to the list of values you want to see in it?

Robin S.
--------------------------
Ah ha! Thanks, that had what I needed. In case anyone else is
wondering, here's what you do:
1. Create a DataGridView with the columns and column types (text,
combobox, checkbox, etc) that you want.
2. Set DataGridView.AutoGenerateColumns to false.
3. Create and fill a DataTable with your data.
4. The important (and non-obvious) part: each existing column in the
DataGridView must be individually connected to its corresponding column
in the DataTable:
DataGridView.Columns[0].DataPropertyName = "Column0";
DataGridView.Columns[1].DataPropertyName = "Column1";
...
5. Set DataGridView.DataSource to the DataTable from 3.

You would think that if AutoGenerateColumns is false, the grid would
automatically connect its columns to the columns of its DataSource, but
I guess it would be too hard to figure out if it's supposed to match
them by position or name or something else, so they left it up to the
programmer.

- John
 

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