开发者

Combobox in bound DataGridView

I've got a DataGridView control which is bound to a database table. I 开发者_开发百科want one of the columns in the gridview to be of combobox type. The combobox should contain a list of hardcoded strings, which is the same for all rows in the datagridview. One of the fields in my database table is an index for this list of hardcoded strings.

I've programatically added a new column to the gridview of type "DataGridViewComboBoxColumn", which successfully creates the column with comboboxes in it. However, that's then not bound to the index field in my DB table.

The index field in my DB table is actually automatically bound to a column via the DataAdapter::Fill method. I've set this column to hidden, so it's hidden to the user.

Obviously just before updating the dataadapter, I can programatically fixup the hidden column in my datatable with the SelectedIndex of my combobox. Just wondering if there's a better way of doing this?

[edit]

Actually rather than doing it just before updating the dataadapter, I'm probably best using the comboboxes cell change event to update the hidden column. Not sure if I'm answering my own question here, or if there's a better way of doing this. It does seem to work, but just feels a bit of a workaround. [/edit]

Thankyou for any help with this,

Dan.


If you need the SelectedValue of the ComboBox to be an index rather than a string, fill the ComboBox with a list of custom objects rathen than a list of strings :

dgvComboColumn.DataSource = new []
{
    new { Index = 1, Text = "Foo" },
    new { Index = 2, Text = "Bar" },
    new { Index = 3, Text = "Baz" }
};
dgvComboColumn.DisplayMember = "Text";
dgvComboColumn.ValueMember = "Index";
dgvComboColumn.DataPropertyName = "(the DB column that stores the index)";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜