2

I have a big problem that make me so confuse that I have a DataGridView without using binding which has DataGridViewComboBoxColumn (unbound column) and I want to get selected index or selected item in the ComBoBoxCell (my item i custom item).

I try to cast or follow this website (http://satishjdotnet.blogspot.com/2009/05/getting-selected-value-of-combo-box-in.html) but I only recieve Error:

"Value is not invalid"

. So how can I solve it? Please help me. Thanks a lot. Here is my custom Item in combobox:

public class CustomItem {
    public string Text { get; set; }
    public object Value { get; set; }
    public override string ToString() {
        return Text;
    }
    public CustomItem(string text, object value) {
        this.Text = text;
        this.Value = value;
    }
}

and how I add it to DataGridViewComboBoxColumn:

List<CustomItem> teamItem = new List<CustomItem>();
teamItem.Add(new CustomItem(this._homeTeam["Name"].ToString(), Convert.ToInt32(this._homeTeam["Id"])));
teamItem.Add(new CustomItem(this._awayTeam["Name"].ToString(), Convert.ToInt32(this._awayTeam["Id"])));
foreach (CustomItem i in teamItem) {
    ((DataGridViewComboBoxColumn)this.dataGridViewGoalInformation.Columns["Team"]).Items.Add(i);
}
Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40
Justin Văn
  • 41
  • 1
  • 7
  • DataGridViewComboBoxCell c = new DataGridViewComboBoxCell(); c = DataGridView1.Rows(0).Cells("MyColumnName"); c.Items.Add("Item1") – Carl Prothman May 17 '15 at 02:52
  • @CarlProthman I want to get selected index or selected item in my combobox cell as I said. and I showed my CustomItem in code too. DO you have any Idea for me ? – Justin Văn May 17 '15 at 02:55
  • Sorry, misread your question. Take a look at this answer... http://stackoverflow.com/questions/4732263/how-to-get-selectedvalue-from-datagridviewcomboboxcolumn – Carl Prothman May 17 '15 at 02:59
  • but what event in datagridview must I catch ? – Justin Văn May 17 '15 at 03:00
  • You could access it during a button on-click occurs (e.g SaveButton). Then access the control's selected value. – Carl Prothman May 17 '15 at 03:03
  • I've just done it but I still get error "Value is not invalid" private void dataGridViewGoalInformation_CellContentClick(object sender, DataGridViewCellEventArgs e) { CustomItem item = this.dataGridViewGoalInformation.Rows[e.RowIndex].Cells["Team"].Value as CustomItem; MessageBox.Show(item.Text); } – Justin Văn May 17 '15 at 03:04
  • What code are you using to access the control's value? – Carl Prothman May 17 '15 at 03:05
  • I have just posted it. I used CellContentClick event – Justin Văn May 17 '15 at 03:10
  • possible duplicate of [Get DatagridviewComboBoxCell's SelectedIndex](http://stackoverflow.com/questions/30157239/get-datagridviewcomboboxcells-selectedindex) – TaW May 17 '15 at 06:40

1 Answers1

9

Given the CustomItem class, with the Value as an int

 public class CustomItem
 {
    public string Text { get; set; }
    public int Value { get; set; }
    public override string ToString()
    {
        return Text;
    }

    public CustomItem(string text, int value)
    {
        this.Text = text;
        this.Value = value;
    }
}

To get the value, make sure to hook up the event: EditingControlShowing

dataGridView1.EditingControlShowing += dataGridView1_EditingControlShowing;

Then to get the value out of the combobox when it changes: 1) get the combobox control, 2) then get it's selected value:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (dataGridView1.CurrentCell.ColumnIndex == 0 && e.Control is ComboBox)
    {
        ComboBox comboBox = e.Control as ComboBox;
        comboBox.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
    }
}

private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    DataGridViewComboBoxEditingControl dataGridViewComboBoxEditingControl = sender as DataGridViewComboBoxEditingControl;
    object value = dataGridViewComboBoxEditingControl.SelectedValue;
    if (value != null)
    {
        int intValue = (int)dataGridViewComboBoxEditingControl.SelectedValue;

        //...
    }
}
Carl Prothman
  • 1,461
  • 13
  • 23
  • I was not able to get the selected value with `dataGridViewComboBoxEditingControl.SelectedValue`, but I was able to get the selected value with `dataGridViewComboBoxEditingControl.Items[dataGridViewComboBoxEditingControl.SelectedIndex]`. – user182917 Apr 19 '20 at 13:07