Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Operation did not succeed because the program cannot commit or quit a cell value change.

this error arises in datdgridview

how it is solved

pls its urgent

C#
private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
      {
          try
          {
              dataGridView1.Refresh();
             Console.WriteLine( dataGridView1.Rows.Count);
              int Secondrowindex = Convert.ToInt32(dataGridView1.CurrentRow.Index);
              int rowcounter = dataGridView1.Rows.Count;
              int asd = Convert.ToInt32(dataGridView1.CurrentCell.ColumnIndex);
              if (asd <= 3 && Secondrowindex == rowcounter - 1)
              {
                  if (e.KeyCode == Keys.Enter)
                  {
                      if (asd == 3)
                      {
                          dataGridView1.CurrentCell = dataGridView1[dataGridView1.CurrentCell.ColumnIndex - 2, dataGridView1.CurrentRow.Index];
                      }
                      else if (asd == null)
                      {
                      }
                      else
                      {
                          dataGridView1.EndEdit();
                          string asew = dataGridView1.EditMode.ToString();
                          dataGridView1.CurrentCell = dataGridView1[dataGridView1.CurrentCell.ColumnIndex + 1, dataGridView1.CurrentRow.Index];
                      }
                  }
              }
              else if (asd == 3 && rowcounter == Secondrowindex + 1)
              {
                  if (e.KeyCode == Keys.Enter)
                  {
                      dataGridView1.CurrentCell = dataGridView1[dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex + 1];
                  }
              }
              else
              {
              }
          }
          catch (Exception gf)
          {
             MessageBox.Show(gf.Message);
          }
      }



here exception comes
Posted

Hi,

Please refer to the following thread[^] from MSDN Forum.

Kind regards,
 
Share this answer
 
I was getting this error in the following case:

1. Have a grid of say 20 rows.
2. My active cell was on say row 10.
3. I would filter the grid down to something less than 10 rows based on a simple find feature I have.
4. This caused the error because the current cell address no longer existed.

Now you'd think the grid would be smart enough to move the current cell to something that is available - but it seems that it doesn't do this.

My fix is to just set the current cell to the first column and first row before changing the number of rows:

C#
dgv.CurrentCell = dgv[0, 0];


That solved this problem for me.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900