DataSets: processing data

  • Thread starter Thread starter Sweetiecakes
  • Start date Start date
S

Sweetiecakes

Yet another newbie question. I have a dataset of an table: I need to
list the values in the Windows Forms application.

A ComboBox, more specifically. The table does not contain any directly
user-readable data: how would I "customize" what the ComboBox's
SelectedText says to a custom format, which could contain data from the
table's fields?

Thanks
 
Yet another newbie question. I have a dataset of an table: I need to
list the values in the Windows Forms application.

A ComboBox, more specifically. The table does not contain any directly
user-readable data: how would I "customize" what the ComboBox's
SelectedText says to a custom format, which could contain data from the
table's fields?

You should bind the DataGridViewComboBoxColumn to a data source (via
its DataSource property), which contains mappings between display
text, and actual values. For example, assuming you have integer values
stored in the database:

Array mapping = new[] {
new { Text = "Red", Value = 1 },
new { Text = "Green", Value = 2 },
new { Text = "Blue", Value = 3 }
} ;

column.DataSource = mapping;
column.DisplayMember = "Text";
column.ValueMember = "Value";

Now the actual value read from or written into the datasource will be
whatever is in the Value property, but the text displayed in the cell
will be taken from the corresponding Text property.
 
Hi. The dataset was filled with an SQLDataAdapter. From what I can see,
this is not possible to do in this case.

Pavel said:
Yet another newbie question. I have a dataset of an table: I need to
list the values in the Windows Forms application.

A ComboBox, more specifically. The table does not contain any directly
user-readable data: how would I "customize" what the ComboBox's
SelectedText says to a custom format, which could contain data from the
table's fields?

You should bind the DataGridViewComboBoxColumn to a data source (via
its DataSource property), which contains mappings between display
text, and actual values. For example, assuming you have integer values
stored in the database:

Array mapping = new[] {
new { Text = "Red", Value = 1 },
new { Text = "Green", Value = 2 },
new { Text = "Blue", Value = 3 }
} ;

column.DataSource = mapping;
column.DisplayMember = "Text";
column.ValueMember = "Value";

Now the actual value read from or written into the datasource will be
whatever is in the Value property, but the text displayed in the cell
will be taken from the corresponding Text property.
 
Hi. The dataset was filled with an SQLDataAdapter. From what I can see,
this is not possible to do in this case.

It doesn't matter where the dataset was filled from (and indeed,
whether the data source is even a dataset). Note that I'm talking
about binding the _combobox column_ to an array, not binding the
entire DataGridView to something. You can very well do both things at
the same time.
 
Back
Top