Question

I have a DatagridviewCombobox Column and am creating DatagridviewCombobox Cells on each row and adding items to it. When I change the value of any (combobox) cell, it throws an exception saying that Datagridviewcombobox cell value is not valid. and the cell value becomes '1'.

I am working on the datagridview_currentcelldirtystatechange event, but haven't been able to make it work.

The code below is creating rows and filling combobox cells with a sequence numbers.

    int _rowLimit =1;

    for (int i = _rowLimit - 1; i < _rowLimit; i++)
    {          
         datagridview.Rows.Add();
         item = i + 1;
         datagridview[myColumn, i].Value = _rowLimit;                   

         DataGridViewComboBoxCell oCell = datagridview.CurrentRow.Cells[myColumn] as DataGridViewComboBoxCell;
         oCell.Items.Add(item);

         ((DataGridViewComboBoxColumn)datagridview.Columns[myColumn]).Items.IndexOf(_rowLimit);
         ((DataGridViewComboBoxColumn)datagridview.Columns[myColumn]).Items.Insert(index, item);
    }

And below is what i am doing in datagridview_currentcelldirtystatechange event:

for (int innerIndex = 0; innerIndex < datagridview.Rows.Count; innerIndex++)
    {
      long sequence = 3;
      long oldSequence = 2;
      long tempValue= Convert.ToInt64(datagridview.Rows[innerIndex].Cells[myColumn].Value);
      if (tempValue <= sequence && tempValue> oldSequence)
      {                    
           datagridview.Rows[innerIndex].Cells[myColumn].Value = tempValue+ 1; // increment   the sequence 
// value here i am getting is correct , but it doesn't show in the DatagridviewCombobox cell where it gets changed of gridview and the mentioned exception is thrown.

      }

Any help would be appreciated. Thanks.

Was it helpful?

Solution

the error on selectedindexChange value of the combobox cells and the exception of DataGridviewComboboxcell value is not valid .. that automatically did change the selected vlue to '1'.. i fixed this issue by adding the DatagridviewComboBoxColumn property in the designer file.

this.columnName.ValueType = typeof(long); 

typeof(long) // this is what what i wanted to show the value in the datagridviewcombobox column.

Issue has now resolved. Thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top