JackA

I have with a number of different controls on it that are bound to two BindingSources. This form allows a user to select a value from a bound ListBox and then it populates a DataGridView which then allows the user to select an item from the DataGridView which populates the rest of the form.

If the user changes a value for one of the loaded items I want to be able to respond to them navigating away from the record and prompt them to save. Additionally I want to be able to turn on and off the Save button.

I've got an event handler RowChanged but it fires too often to be useful. It fires when rows are added to the table from the dataset on initial load from the database. This makes sense, but isn't what I want. I want to be able to set some sort of "dirty" bit or something when a user changes a row or adds a new row and prompt them to save.

Is there a good way to approach this or is there a way to ignore the initial loading events

Thanks
Jack


Re: Windows Forms Data Controls and Databinding Responding to changes in a row and form.

Ken Tucker

Maybe DatagridView's RowValidated or RowValidating events will work better.




Re: Windows Forms Data Controls and Databinding Responding to changes in a row and form.

JackA

I just took a look at RowValidating and it might work, however I'm having a problem that I've encountered with all events that fire when something changes. The events fire when the control is a databound. Is there a way to detect when a control is initially databound so I can bypass my checks

Alternately, is there a way to determine when the USER makes the changes rather than the system




Re: Windows Forms Data Controls and Databinding Responding to changes in a row and form.

Zhi-Xin Ye - MSFT

Handle the CurrentCellDirtyStateChanged event will do the trick for you.

Code Snippet

public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

}

private void Form2_Load(object sender, EventArgs e)

{

DataTable dt = new DataTable();

dt.Columns.Add("col1");

dt.Columns.Add("col2");

dt.Rows.Add("aa","bb");

dt.Rows.Add("nn","dd");

dt.Rows.Add("dd","ee");

this.dataGridView1.DataSource = dt;

this.button1.Enabled = false;

this.dataGridView1.CurrentCellDirtyStateChanged += new EventHandler(dataGridView1_CurrentCellDirtyStateChanged);

}

void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)

{

this.button1.Enabled = true;

}

private void button1_Click(object sender, EventArgs e)

{

// some code here

this.button1.Enabled = false;

}

}