none
Return which rows have been edited in a DataGridView item

    Question

  • Right now I'm trying to figure out how to figure out which rows have been edited in the DataGrid's built in editor.

    In short, I want to change a property in the Database I am using to show when this specific element has been modified (Date Modified kind of deal).

    The best way I am going about this is having it so that when I am done editing the item, I want to save a list of the row indexes, then when I am done editing, and the user hits the "Update" button, it iterates through the list, changing those values to the current Date-time.

     

    How would I go about doing this? DataGrids have a function I am trying to use, CellEndEdit, and I would like to know how to keep track of someone changing the item, and how to return the row number.

    • Moved by OmegaManMVP Thursday, July 14, 2011 9:36 PM (From:Visual C# General)
    Thursday, July 14, 2011 7:13 PM

Answers

  • Hi vanalmelo,

    There are many ways to do this. But, here I am using CellBeginEdit and CellEndEdit events for this. Please see the code below.

    List<int> modifiedRows = new List<int>();
        
    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
          if(dataGridView1.CurrentCell.Tag != dataGridView1.CurrentCell.Value)
          {
              if (!modifiedRows.Contains(e.RowIndex))
              {
                    modifiedRows.Add(e.RowIndex);
              }
          }
    }
    
    private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
          dataGridView1.CurrentCell.Tag = dataGridView1.CurrentCell.Value;
    }
    


    Now, modifiedRows list contains all the rows which are modified.

    Hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Friday, July 15, 2011 7:50 AM
  • Hi vanamelo,

       In addtion to Advesh's solution, if you are hanging any data programmatically and would like to keep update of those rows also, then I would add event for CellValueChanged also:

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
          if (modifiedRows.Contains(e.RowIndex))
            modifiedRows.Add(e.RowIndex);
        }
    
    

    Remember, all these event will mostly be executed even when you load data to the datagrid. For that I would prefer having a

    bool loading = false;
    
    loading = true;
    datagridValue.DataSource = dt;
    
    // when finished loading data in to the grid
    loading = false;
    
    

    With this, in each event, first add

    if (loading)

       return;

    This will preent the code to be executed while loading is going on. Hope this helps.

     

     


    Thanks
    If you find any answer helpful, then click "Vote As Helpful" and if it also solves your question then also click "Mark As Answer".
    Friday, July 15, 2011 11:20 AM

All replies

  • Hi vanalmelo,

    There are many ways to do this. But, here I am using CellBeginEdit and CellEndEdit events for this. Please see the code below.

    List<int> modifiedRows = new List<int>();
        
    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
          if(dataGridView1.CurrentCell.Tag != dataGridView1.CurrentCell.Value)
          {
              if (!modifiedRows.Contains(e.RowIndex))
              {
                    modifiedRows.Add(e.RowIndex);
              }
          }
    }
    
    private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
          dataGridView1.CurrentCell.Tag = dataGridView1.CurrentCell.Value;
    }
    


    Now, modifiedRows list contains all the rows which are modified.

    Hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Friday, July 15, 2011 7:50 AM
  • Hi vanamelo,

       In addtion to Advesh's solution, if you are hanging any data programmatically and would like to keep update of those rows also, then I would add event for CellValueChanged also:

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
          if (modifiedRows.Contains(e.RowIndex))
            modifiedRows.Add(e.RowIndex);
        }
    
    

    Remember, all these event will mostly be executed even when you load data to the datagrid. For that I would prefer having a

    bool loading = false;
    
    loading = true;
    datagridValue.DataSource = dt;
    
    // when finished loading data in to the grid
    loading = false;
    
    

    With this, in each event, first add

    if (loading)

       return;

    This will preent the code to be executed while loading is going on. Hope this helps.

     

     


    Thanks
    If you find any answer helpful, then click "Vote As Helpful" and if it also solves your question then also click "Mark As Answer".
    Friday, July 15, 2011 11:20 AM