简体   繁体   English

如何在数据绑定的DataGridViewComboBox中显示属性而不是值?

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

I'm new to C# and .NET 我是C#和.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. 我需要在数据绑定的DatagridViewComboBox中显示值的匹配名称,但是我不知道该怎么做。

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. GetStateName是CR类的属性,该属性返回CR状态的匹配名称。 I need to display the state name in the combo box. 我需要在组合框中显示状态名称。 How to do that? 怎么做? Thanks. 谢谢。

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. 如果要显示与单元格应包含的值不同的内容,则不能简单地将一件事加载到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 数据的可视表示形式的字段,称为DisplayMember
  • And a field for the actual data values, called ValueMember 实际数据值的字段称为ValueMember

These fields can sit in a DataTable but you can also use any other collection with suitable properties. 这些字段可以位于DataTable但是您也可以使用具有适当属性的任何其他集合。

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. 在示例中,我手动加载了Items 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 . 您可以通过如上所述加载数据源列表来做到这一点,也可以独立地或在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 . 如果所有单元格都需要具有单独的查找值,则需要分别加载它们,而不使用列,而是将每个单元格强制转换为DataGridViewComboBoxCell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM