0

How do i set tag for each element in my DataGridViewComboBox cell

my DataGridViewComboBox cell has the following items:

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

I want to add seperate tags for Apple,Orange,Mango

Prashanth
  • 11
  • 2

1 Answers1

0

Affraid you cannot do it through DataGridViewComboBoxCell.

But, if you want keep some separate information about items added to ComboBox collection, then you can create own class of DataGridViewComboBoxCell elements and add the instances of this class to DataGridViewComboBoxCell as a list

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;
    }
}

Then you can add list of Fruits to DataGridViewComboBoxCell, but before you will need to create a Fruits with your information

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);
}

After this your can use your tag information as:

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;
}
Fabio
  • 31,528
  • 4
  • 33
  • 72