Datagrid with dropdowns

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

In a dataview, I have lots and lots of table relations that make editing
each row kind of a hassle... having to look up ID numbers in 7 different
tables to reference the correct items in the parent tables. And then, once
entered, it's almost impossible to read.

Is there a way to have each field that has a relationship to a table display
the possible choices in a dropdown list? That would be most useful.
 
In a dataview, I have lots and lots of table relations that make editing
each row kind of a hassle... having to look up ID numbers in 7 different
tables to reference the correct items in the parent tables. And then, once
entered, it's almost impossible to read.

Is there a way to have each field that has a relationship to a table display
the possible choices in a dropdown list? That would be most useful.

To get you started:

1) Create a DataGridView object and set it to NOT automatically
generate the columns: AutoGenerateColumns = false

2) Create a DataGridViewComboBoxColumn object...
Set it's DataSource to the table of "valid values" (child table)...
Set its DisplayMember property to the column name in that table of the
description that you want to see in the drop down...
Set its ValueMember property to the primary key column name or (other
column that represents the foreign key in the parent table column)...
Set the DataPropertyName to the name of the column in the parent table
that corresponds to the ValueMember column in the combo box (this is
the parent column that you want to update with the combo selections).

3) Add the combo box column to the data grid

4) Set the DataSource of the data grid view to the parent table

Now instead of seeing arbitrary primary key integers in your columns
you will see that the combo boxes show the proper descriptions and can
be used to update the foreign key integers in the parent table.

Opinion: it is always better to turn off auto column generation with
the DGV, leaving it on will cause the grid to throw many data bind
complete events as you add combo boxes and have to then remove the
auto generated text boxes for the same FK columns. Auto generate is
really, IMO just for RAD prototyping, as once your code starts to
listen to more and more grid events auto generation causes a lot of
"thrashing" that gets complicated to handle. I find it best to
manually add my own custom columns based on the columns collection of
the underlying data source at object construction time.

HTH
Rick
 
Back
Top