Search code examples
c#datagridviewcellcounter

Cell/Row Counting


I have a DataGridView that contains 13 columns. Most of the rows are completely full but some rows have empty columns. For each empty column there is an emptyCellCounter that keeps track of how many empty cells there are.. and for each row that has an empty cell there is an emptyRowCounter.

So there could be 15 empty rows and 89 empty cells. When a user edits a cell I want to recalculate the emptyCellCounter as well as the emptyRowCounter using my "lineCounter" function.


QUESTION

  • How do I change this counter when a cell is edited?

Solution

  • You can use events to know when the values of the cells have changed. Look at the events on the properties window when you have your datagrid selected. Then double click in the cell and VS will add a default event for you in the code.

     private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
      {
         lineCounter();
      } 
    

    EDIT:

    Note that Davide Piras way of using the CellValidated event could be better for you. You may not want to call the lineCounter method if the cell is invalid.