Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 每个项目的DataGridViewComboBox单元格设置标记_C#_Datagridview_Datagridviewcomboboxcell - Fatal编程技术网

C# 每个项目的DataGridViewComboBox单元格设置标记

C# 每个项目的DataGridViewComboBox单元格设置标记,c#,datagridview,datagridviewcomboboxcell,C#,Datagridview,Datagridviewcomboboxcell,如何为DataGridViewComboBox单元格中的每个元素设置标记 我的DataGridViewComboBox单元格包含以下项: string[] Fruits = {"Apple", "Orange","Mango"}; for (i=0;i<3;i++) { DataGridViewComboBoxCellObject.Items.Add(Fruits[i]); //Set a seperate tag for this item } string[]Frui

如何为DataGridViewComboBox单元格中的每个元素设置标记

我的DataGridViewComboBox单元格包含以下项:

string[] Fruits = {"Apple", "Orange","Mango"};
for (i=0;i<3;i++)
{
    DataGridViewComboBoxCellObject.Items.Add(Fruits[i]);
    //Set a seperate tag for this item
}
string[]Fruits={“苹果”、“橘子”、“芒果”};

对于(i=0;iafraid,您无法通过DataGridViewComboxCell执行此操作

但是,如果希望保留有关添加到ComboBox集合的项的一些单独信息,则可以创建自己的DataGridViewComboBoxCell元素类,并将此类的实例作为列表添加到DataGridViewComboxCell

public class Fruit
{
    public String Name {get; set;}
    public Object Tag {get; set;} //change Object to YourType if using only one type

    public Fruit(String sInName, Object inTag)
    {
        this.Name=sInName;
        this.Tag=inTag;
    }
}
然后,您可以将水果列表添加到DataGridViewComboxCell,但在此之前,您需要使用您的信息创建水果列表

string[] Fruits = {"Apple", "Orange","Mango"};
for (i=0;i<3;i++)
{
    Object infoData; //Here use your type and your data
    //Create a element
    Fruit temp = New Fruit(Fruits[i], infoData);
    //Add to DataGridViewComboBoxCell
    DataGridViewComboBoxCell.Items.Add(temp);
}
if (this.DataGridView.Rows[0].Cells[DataGridViewComboBoxCell.Name].Value != null)
{
    Fruit fruit = (fruit)this.DataGridView.Rows[0].Cells[DataGridViewComboBoxCell.Name].Value;
    Object tagValue = fruit.Tag;
}