Groups | Blog | Home
all groups > dotnet windows forms databinding > november 2006 >

dotnet windows forms databinding : Simple-binding DataGridViewComboBoxColumn to DataTable


Steffen Skov
11/23/2006 5:01:45 PM

Hi all,

can anyone tell me whether it is possible to use simple binding between
the selected value of a DataGridViewComboBoxColumn and a DataTable column?

My specific scenario is something like this:

I have a strongly typed DataTable (MTGUIData), which contains various
columns of type System.Double, System.String or System.Int32.

In my form I use a DataGridView control, whose columns are explicitly
bound to columns in the DataTable. Mostly, the DataGridView columns are
of type DataGridViewTextBoxColumn, which bind nicely to the
corresponding columns in my table.

However, one of the DGV columns is a DataGridViewComboBoxColumn, and I
want to enter the Items of the ComboBox column at design-time (thus, no
binding of the items of the ComboBox), and bind the index of the
selected item of the ComboBox to a System.Int32 type column in the
DataTable.

One could say that what I want to accomplish looks a bit like what I do
with DataGridViewCheckBoxColumn objects. They have a "TrueValue",
"IndeterminateValue" and "FalseValue" attributes, which seems to be used
to map the states of the checkbox to values in the data table row.


--

Kind regards

Steffen Skov
Senior consultant
Bart Mermuys
11/23/2006 9:59:48 PM
Hi,

[quoted text, click to view]

You can bind SelectedValue using the DataPropertyName to link the value to a
column.

[quoted text, click to view]

SelectedIndex no, that's not supported.

But you could create your own DataGridViewComboBoxColumn/Cell which does
this. After compilation you can use this column inside the DataGridView
column editor. You would just setup your items and set DataPropertyName to
an integer column, eg. :

public class DataGridViewComboBoxColumnIdx : DataGridViewComboBoxColumn
{
public DataGridViewComboBoxColumnIdx()
{
CellTemplate = new DataGridViewComboBoxCellIdx();
}
}

public class DataGridViewComboBoxCellIdx : DataGridViewComboBoxCell
{
protected override object GetFormattedValue(object value, int rowIndex,
ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter
valueTypeConverter, System.ComponentModel.TypeConverter
formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
if (value != null && value != cellStyle.DataSourceNullValue)
{
int idx = (int)value;

if (idx >= 0 && idx < Items.Count)
return Items[idx].ToString();
else
return "Error - Index out of range";
}
else
return string.Empty;
}

public override object ParseFormattedValue(object formattedValue,
DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter
formattedValueTypeConverter, System.ComponentModel.TypeConverter
valueTypeConverter)
{
int idx;

for (idx = 0; idx < Items.Count; ++idx)
{
if (Items[idx].ToString() == (string)formattedValue)
break;
}

return idx;
}
}


Offcourse, you could change your design and put these items in a DataTable
(string, value) where in your case value would be incrementing and then bind
the DataTable to the ComboBox and then use the normal SelectedValue binding.


HTH,
Greetings




[quoted text, click to view]

Steffen Skov
11/24/2006 3:50:02 PM
[quoted text, click to view]
[snip]

Thank you for your help! I more or less copied your code into my source,
and it worked like a charm.

--

Kind regards

Steffen Skov
Senior Consultant
AddThis Social Bookmark Button