Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The CellEndEdit event is running when I edit the datagridview cells.
but when I print the data from the database to the DataGridView cell, CellEndEdit does not work.
C#
dataGridView1.Rows[0].Cells[0].Value = "test";
How can I run CellEndEdit? THANKS...

Codes:
private void btnKaydet_Click(object sender, EventArgs e)
        {
            try
            {
                int index = 2;
                int rc = gridView1.RowCount;
                for (int i = 0; i < rc; i++)
                {
                    index++;
                    string stk = gridView1.GetRowCellValue(i, "STOKKODU").ToString();
                    string beden = gridView1.GetRowCellValue(i, "BEDEN").ToString();
                    string mik = gridView1.GetRowCellValue(i, "MIKTARI_2").ToString();
                    dataGridView1.Rows[index].Cells[0].Value = stk;
                    dataGridView1.Rows[index].Cells[1].Value = beden;
                    dataGridView1.Rows[index].Cells[2].Value = mik;
                }
                ExcelProje.Save();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }



private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            object hucre = ExcelSayfa.Cells[dataGridView1.CurrentCell.RowIndex + 2, dataGridView1.CurrentCell.ColumnIndex + 1];
            Excel.Range bolge = ExcelSayfa.get_Range(hucre, hucre);
            bolge.Value2 = dataGridView1.CurrentCell.Value;
        }


What I have tried:

datagridview1.EndEdit();

-----------------------

dataGridView1.CellEndEdit += DataGridView1_CellEndEdit;

private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
       {
          object hucre = ExcelSayfa.Cells[dataGridView1.CurrentCell.RowIndex + 2, dataGridView1.CurrentCell.ColumnIndex + 1];
           Excel.Range bolge = ExcelSayfa.get_Range(hucre, hucre);
           bolge.Value2 = dataGridView1.CurrentCell.Value;
       }
Posted
Updated 18-Dec-20 20:27pm
v2

1 solution

I think you need to use
C#
dataGridView1.Rows[e.RowIndex]
Also see: DataGridView.CellEndEdit Event (System.Windows.Forms)[^]
The CellEndEdit event will probably only fire after the cell has lost focus.
So maybe it's better to use the CellValueChanged event instead, in my test it worked when changing a cell value programmatically.
Another option is to use the CurrentCellDirtyStateChanged event, see: winforms - Cell Value Changing Event ,c# - Stack Overflow[^]
 
Share this answer
 
v3
Comments
Adilcan Topuz 28-Jul-17 2:11am    
dataGridView1[0][0].value = "test";

I tried this, but CellValueChanged did not work. :)
RickZeeland 28-Jul-17 2:22am    
Maybe you have some conflicting properties set, could you try with a fresh form and see if it works then ?
Adilcan Topuz 28-Jul-17 2:49am    
I try now :)
Adilcan Topuz 28-Jul-17 3:02am    
I am getting the same mistake again.

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