DataSets: processing data

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
 
P

Pavel Minaev

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.
 
S

Sweetiecakes

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.
 
P

Pavel Minaev

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top