0

I have a DataGridView that I want users to be able to add records to directly. This is done by clicking a link beneath the DGV, at which point a new row is programmatically added, and the first visible cell is of ComboBoxCell type. Code excerpt for how I'm doing this is:

// Add a new row to DGV
DataGridView dgv = this.dgvInformation;
DataGridViewRow newRow = new DataGridViewRow();
dgv.Rows.Add(newRow);

// Create cells and add to row
DataGridViewComboBoxCell cellInfoType = new DataGridViewComboBoxCell();
newRow.Cells["InfoType"] = cellInfoType;

// Create DataSource based off LINQ query here
List<ComboItemAccountInfoType> comboDataSource = new List<ComboItemAccountInfoType>();
// List is populated here

// Assign DataSource to combo cell
cellInfoType.DataSource = comboDataSource;
cellInfoType.ValueMember = "AccInfoTypeID";
cellInfoType.DisplayMember = "InfoType";

// Scroll new row into view and begin editing
dgv.FirstDisplayedScrollingRowIndex = dgv.Rows.Count - 1;
dgv.CurrentCell = dgv[1, dgv.Rows.Count - 1];
dgv.BeginEdit(true);

The DataSource contains some values with an ID of -1, which are categories that the user should not be able to select, and all other values have their valid database ID. This all works fine, except that if a user has selected a -1 row I am unable to keep the combo cell in edit more, as it simply stores the value in the cell and I can no longer activate the drop-down. I have added the following code to the _CellValueChanged event, but it has no effect:

private void dgvInformation_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    DataGridView dgv = this.dgvInformation;
    DataGridViewCell cell = dgv[e.ColumnIndex, e.RowIndex];

    // If type of cell is ComboBox then 
    if (cell.GetType() == typeof(DataGridViewComboBoxCell))
    {
        DataGridViewComboBoxCell cellInfoType = (DataGridViewComboBoxCell)cell;

        if ((int)cellInfoType.Value == -1)
        {
            MessageBox.Show("Going back into edit mode...");
            dgv.CurrentCell = cell;
            dgv.BeginEdit(true);
        }
        else
        {
            MessageBox.Show(cellInfoType.Value.ToString());
        }
    }
}

I do get the "Going back into edit mode..." message here after moving onto another cell, but it doesn't then do what it says! Could anyone please explain why it won't go back into edit mode, or is there a way of preventing the value becoming locked as soon as it has been selected?

Many thanks!

Niall
  • 1,551
  • 4
  • 23
  • 40
  • Question is not easily understandable. Datagridview is editable by default. Why so you need to use `dgv.BeginEdit(true);` ? – Sami Sep 20 '12 at 14:10
  • I have the DGV EditMode set to EditProgrammatically, as I only want users to be able to update on double-click, or when the Add Item link is clicked. Also, the editable column is displayed as a TextBox, and is only a Combo when being edited – Niall Sep 20 '12 at 14:16

1 Answers1

1

There should be a "validating" event for you to intercept and cancel if the value is -1.

Lindan
  • 110
  • 9
  • I only seem to be able to query the FormattedValue, which is the text displayed, do you know if it's possible to query the actual value? I can work with the text, but it would be more convenient to be able to query the ID – Niall Sep 20 '12 at 15:13
  • I have now found the answer to this, you have to cast the DGV's EditingControl as a ComboBox (not DataGridViewComboBoxCell) and you can then reference it's SelectedValue property – Niall Sep 21 '12 at 10:29