user315561
user315561

Reputation: 671

How to display a property instead of the value in a databound DataGridViewComboBox?

I'm new to C# and .NET

I need to display the matching name of a value in a databound DatagridViewComboBox, but I can't figure out how to do that.

I have the following code:

bs = new BindingSource();
bs.DataSource = typeof(CR);

dataGridView1.AutoGenerateColumns = false;
Column1.Width = 400;
Column1.DataPropertyName = "CR_NAME";
Column2.DataPropertyName = "CR_STATE_S";
Column2.ValueMember = "CR_STATE_S";
Column2.DisplayMember = "GetStateName";

Column2.Items.Add("0"); // how to set the matching value here?
Column2.Items.Add("1");
Column2.Items.Add("2");

dataGridView1.DataSource = bs;

GetStateName is a property of the CR Class that returns the matching name of the CR state. I need to display the state name in the combo box. How to do that? Thanks.

Upvotes: 0

Views: 60

Answers (1)

TaW
TaW

Reputation: 54433

If you want to display something different from the values the cells shall contain, then you can't simply load one thing into the Items of the ComboBoxCells.

Instead you need a DataSource that has at least different fields for the two things you want to use:

  • A field for the visible representation of the data, called DisplayMember
  • And a field for the actual data values, called ValueMember

These fields can sit in a DataTable but you can also use any other collection with suitable properties.

Lets create a very simple class and have a List of that class:

class itemClass
{
    public string display { get; set;}
    public string value { get; set; }
    public itemClass(string d, string v) 
    { display = d; value = v;}
}

List<itemClass> myItems = new List<itemClass>();

private void loadButton_Click(object sender, EventArgs e)
{
    // load the list with all values:
    myItems.Add(new itemClass("zero", "0"));
    myItems.Add(new itemClass("one", "1"));
    myItems.Add(new itemClass("two", "2"));
    myItems.Add(new itemClass("three", "3"));
    myItems.Add(new itemClass("four", "4"));
    myItems.Add(new itemClass("five", "5"));
    myItems.Add(new itemClass("six", "6"));

    // prepare the DataGridView 'DGV':
    DGV.Columns.Clear();
    DataGridViewComboBoxCell cCell = new DataGridViewComboBoxCell();

    DataGridViewComboBoxColumn cCol = new DataGridViewComboBoxColumn();
    DGV.Columns.Add(cCol);
    cCol.DisplayMember = "display";
    cCol.ValueMember = "value";
    cCol.DataSource = myItems;
    cCol.ValueType = typeof(string);

    // add a few rows, for testing:
    DGV.Rows.Add(7);
    for (int i = 0; i < DGV.Rows.Count; 
        i++) DGV.Rows[i].Cells[0].Value = i + "";
}

In the example I load the Items manually. Usually you will want to pull the values from the database or some other source. You can do that either by loading the datasource list as above or you can have a lookup table either independently or in in a DataSet.

If all cells need to have individual lookup values you need to load them separately and not use the column but each of the cells cast to DataGridViewComboBoxCell.

Upvotes: 1

Related Questions