如何为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
}我想为苹果,橙子,芒果添加单独的标签
发布于 2013-03-13 04:56:48
恐怕你不能通过DataGridViewComboBoxCell做到这一点。
但是,如果希望保留有关添加到ComboBox集合中的项的一些单独信息,则可以创建自己的DataGridViewComboBoxCell元素类,并将该类的实例作为列表添加到DataGridViewComboBoxCell中
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;
}
}然后,您可以将水果列表添加到DataGridViewComboBoxCell,但在此之前,您将需要使用您的信息创建水果
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;
}https://stackoverflow.com/questions/15335770
复制相似问题