Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView Save Changes On Row Change

I am attempting to save a record after leaving the row in a DataGridView.

I have seen solutions use the RowValidated event, however, when the rows are sorted then the record gets resorted before the RowValidation event is fired. I also attempted to get the row using the BindingSource Current property, but the current property is already changed. Same applies to RowLeaving I believe.

I have tried using the RowChanged event on the datatable directly and that does work OK.

I ended up using the RowValidation event and getting the changes from the datatable (GetChange()), but this doesn’t seem idea.

In addition for deletes I used the UserDeletingRow event, but again this doesn’t seem idea.

What is the best way for saving records after leaving the row?

like image 831
user824911 Avatar asked Feb 11 '13 23:02

user824911


2 Answers

Have you tried the DataGridView.RowValidating Event and .IsCurrentRowDirty?

    private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
    {
        if (dataGridView1.IsCurrentRowDirty)
        {
           //Do stuff
        }
    }
like image 51
spajce Avatar answered Oct 17 '22 05:10

spajce


Have you taken a look at .RowLeave?

private void dataGridView1_RowEnter(object sender, 
    DataGridViewCellEventArgs e)
{
    for (int i = 0; i < dataGridView1.Rows[e.RowIndex].Cells.Count; i++)
    {
        dataGridView1[i, e.RowIndex].Style.BackColor = Color.Yellow;
    }
}

private void dataGridView1_RowLeave(object sender, 
    DataGridViewCellEventArgs e)
{
    for (int i = 0; i < dataGridView1.Rows[e.RowIndex].Cells.Count; i++)
    {
        dataGridView1[i, e.RowIndex].Style.BackColor = Color.Empty;
    }
}

source: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowleave.aspx

like image 32
Chris Ballance Avatar answered Oct 17 '22 07:10

Chris Ballance