1

As mentioned above,

How can I disable (or some workaround programatically) this?

some pseudocode could be:

cellvaluechangedevent/rowaddedevent(sender,e){
     If(initial load of datagrid){
          //trap this, end of method
     }
     else{
          //do work
     }
}
voluminat0
  • 866
  • 2
  • 11
  • 21

3 Answers3

2

I tend to separate my data loading into its own method. As such, I can then detach handlers which may be fired during a load or refresh, and then simply re-attach when that process is finished. This also makes it easier (where applicable) to refresh the dgv data from other places in the code.

The key, in the context of your problem, is:

  1. detach handler
  2. fill DataGridView or attach data source, however you are doing that.
  3. re-attach handler
private void LoadDataGrid()
{
    this.dataGridView1.CellValueChanged -= new 
        DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

    // Your code to load data here

    this.dataGridView1.CellValueChanged +=new 
        DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
}

void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    // Your code to handle the cell value changing
}
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
XIVSolutions
  • 4,442
  • 19
  • 24
1

Seems you just want to bypass the first time it fired.
Assume your DataGridView named dataGridView1, and assigned the original handler dataGridView1_CellValueChanged, then you can do:

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
    dataGridView1.CellValueChanged-=dataGridView1_CellValueChanged;

    dataGridView1.CellValueChanged+=
        (sender_, e_) => {
            MessageBox.Show("fired after first time");
        };
}
Ken Kin
  • 4,503
  • 3
  • 38
  • 76
0

You can use DataGridView.IsCurrentCellDirty or IsCurrentRowDirty

cellvaluechangedevent/rowaddedevent(sender,e){
 if (dataGridView1.IsCurrentCellDirty) {
     If(initial load of datagrid){ 
          //trap this, end of method 
     }
     else{
          //do work
     }
   }
 }
spajce
  • 7,044
  • 5
  • 29
  • 44
  • yes, i just copy and paste from your pseudocode then added `if (dataGridView1.IsCurrentCellDirty) {}` :) – spajce Jan 05 '13 at 15:06