Hi,
[quoted text, click to view] "Steffen Skov" <ssk@_at_scandpowerpt_dot_com> wrote in message
news:%23pS5tixDHHA.4832@TK2MSFTNGP06.phx.gbl...
>
> 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?
You can bind SelectedValue using the DataPropertyName to link the value to a
column.
[quoted text, click to view] >
> 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.
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] >
> 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
> Scandpower Petroleum Technology AS