none
Set value for DataGridViewComboBoxCell.Value RRS feed

  • Question

  • Hi All,

    Basically I am adding 1 DataGridViewComboBoxColumn and DataGridViewTextBoxColumn into my DataGridView, below is the code:

    DataGridViewComboBoxColumn newComboBoxColumn = new DataGridViewComboBoxColumn(); 
    newComboBoxColumn.HeaderText = "A";
                
    // If we add items to DataGridViewComboBoxColumn
    newComboBoxColumn.Items.Add("Combo box");
    //
    dataGridView1.Columns.Insert(0, newComboBoxColumn);
    DataGridViewTextBoxColumn newTextColumn = new DataGridViewTextBoxColumn();
    newTextColumn.HeaderText = "B";
    dataGridView1.Columns.Insert(1, newTextColumn);
                
    dataGridView1.Rows.Add(2);
                
    foreach (DataGridViewRow row in dataGridView1.Rows) 
    {
    	DataGridViewComboBoxCell comboBoxCell = (DataGridViewComboBoxCell)(row.Cells[0]);
            if (newComboBoxColumn.Items.Count > 0)
            {
             	// The value of DataGridViewComboBoxCell can be set.
                    comboBoxCell.Value = newComboBoxColumn.Items[0];
                    // 
     	}
            DataGridViewTextBoxCell textBoxcell = (DataGridViewTextBoxCell)(row.Cells[1]);
            textBoxcell.Value = "text box";
    } 

    As per my comments said in the above code, if I added a few items into DataGridViewComboBoxColumn, and then I can set the value of DataGridViewComboBoxCell, but really what i want is like this:comboBoxCell.Value = "Combo box";

    But it does work for textBoxCell.Value. As you know the normal ComboBox control has a "Text" property that we can set value to it, but looks like it is not the case for DataGridViewComboBoxCell.

    Had anyone ever have the same issue and what is the work around.

    Many Thanks.

    Thursday, October 11, 2012 6:48 AM

Answers

  • as promised, this is the code that address my probleam and hope others can benefit from it.

    DataGridViewComboBoxColumn comboStates = new DataGridViewComboBoxColumn(); comboStates.HeaderText = "HeaderText_1"; this.dgvSpecialItem.Columns.Insert(0, comboStates); DataGridViewComboBoxColumn comboCities = new DataGridViewComboBoxColumn(); comboCities.HeaderText = "HeaderText_2"; this.dgvSpecialItem.Columns.Insert(1, comboCities); for (int i = 0; i < dt.Rows.Count; i++) { comboStates.Items.Add(dt.Rows[i][0]); DataGridViewComboBoxCell stateCell = (DataGridViewComboBoxCell)(dgvSpecialItem.Rows[i].Cells[0]); stateCell.Value = comboStates.Items[i]; comboCities.Items.Add(dt.Rows[i][1]); DataGridViewComboBoxCell cityCell = (DataGridViewComboBoxCell)(dgvSpecialItem.Rows[i].Cells[1]); cityCell.Value = comboCities.Items[i]; }


    • Marked as answer by AndieDu Monday, October 15, 2012 11:24 AM
    Monday, October 15, 2012 11:24 AM

All replies

  • You can set the value with in the range of the combobox items so you need to add "Combo box" as item in the combobox cell then you can set the value of the cell.

    you can simplify your code as :

    DataGridViewComboBoxColumn newComboBoxColumn = new DataGridViewComboBoxColumn();
                newComboBoxColumn.HeaderText = "A";
                newComboBoxColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
                // If we add items to DataGridViewComboBoxColumn
               // newComboBoxColumn.Items.Add("Combo box");
                //
                dataGridView1.Columns.Insert(0, newComboBoxColumn);
                DataGridViewTextBoxColumn newTextColumn = new DataGridViewTextBoxColumn();
                newTextColumn.HeaderText = "B";
                dataGridView1.Columns.Insert(1, newTextColumn);
    
                dataGridView1.Rows.Add(2);
    
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    DataGridViewComboBoxCell comboBoxCell = (DataGridViewComboBoxCell)(row.Cells[0]);
                    comboBoxCell.Items.Add("Combo box");
                    comboBoxCell.Value = "Combo box";
                    DataGridViewTextBoxCell textBoxcell = (DataGridViewTextBoxCell)(row.Cells[1]);
                    textBoxcell.Value = "text box";
                } 


    Hirendra Sisodiya from authorcode.com

    • Proposed as answer by Jason Dot Wang Monday, October 15, 2012 8:01 AM
    • Unproposed as answer by Jason Dot Wang Monday, October 15, 2012 8:01 AM
    Thursday, October 11, 2012 7:24 AM
  • thaks but your thing wont work, and i cant see any logic behand your code with my enquries.

    this simple thing wont work, as i stated in my original post.

    comboBoxCell.Items.Add("Combo box");
    comboBoxCell.Value = "Combo box";

    Friday, October 12, 2012 7:04 AM
  • A work around / solution has been found and will be posted into here this evening (aus east stand time)

    Monday, October 15, 2012 12:17 AM
  • as promised, this is the code that address my probleam and hope others can benefit from it.

    DataGridViewComboBoxColumn comboStates = new DataGridViewComboBoxColumn(); comboStates.HeaderText = "HeaderText_1"; this.dgvSpecialItem.Columns.Insert(0, comboStates); DataGridViewComboBoxColumn comboCities = new DataGridViewComboBoxColumn(); comboCities.HeaderText = "HeaderText_2"; this.dgvSpecialItem.Columns.Insert(1, comboCities); for (int i = 0; i < dt.Rows.Count; i++) { comboStates.Items.Add(dt.Rows[i][0]); DataGridViewComboBoxCell stateCell = (DataGridViewComboBoxCell)(dgvSpecialItem.Rows[i].Cells[0]); stateCell.Value = comboStates.Items[i]; comboCities.Items.Add(dt.Rows[i][1]); DataGridViewComboBoxCell cityCell = (DataGridViewComboBoxCell)(dgvSpecialItem.Rows[i].Cells[1]); cityCell.Value = comboCities.Items[i]; }


    • Marked as answer by AndieDu Monday, October 15, 2012 11:24 AM
    Monday, October 15, 2012 11:24 AM
  •  I think this way it will work.

    (grdColumns["ColControl", index] as DataGridViewComboBoxCell).Value  = "TextBox";   

    Sunday, March 9, 2014 1:45 AM