Reputation: 422
My C# winform has a datagridview and when I clicked update button, what code should I put to check whether any cells had been edited?
I just need to have a true or false.
Thanks.
===========================================================================
My existing codes:
#region Edit Records
private void InProSysAdministrationEventsUpdateButton_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Please Click Ok to Edit the Events", "Confirmation", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
ManipulateData.UpdateData(connectionString, tblconn, tblscmd, tbldataadaptor, tbldatatable, cmbuilder, "usp_readallevents", readalleventsdataGridView);
}
}
#endregion
I need to do the following:
1) user click on edit
2) system check whether any cell had been edited
3) if no cell edited, it will messagebox.show("No Changes Done.")
4) else, it will update the database.
Upvotes: 1
Views: 8030
Reputation: 436
int x=0;
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
x = 1;
}
if(x==1) //this means that gridView has been updated
Upvotes: -1
Reputation: 1442
Have you taken a look at the DataGridView.CellValueChanged Event? MSDN
Would be fairly simple to just write a handler for this and set a flag, or perform whatever action you want.
An example of how you might go about performing this would be:
protected override void OnLoad(EventArgs e)
{
myDataGridView.CellValueChanged += new DataGridViewCellEventHandler(
myDataGridView_CellValueChanged);
}
private void myDataGridView_CellValueChanged(
object sender, DataGridViewCellEventArgs e)
{
//some very crude examples of actions you might want to perform when the event handler is triggered.
myObject.update();
//or something else like
myObject.isUpdatable = true;
}
With regards to point number 3, a msgbox is probably not the best way to inform a user of a non-critical event. It is likely that they are already going to know that they haven't entered any information, and you can provide this feedback less anoyying ways by perhaps flagging required cell, or whatever. Food for thought.
In the future, I'd recommend searching MSDN for the class you are working with, and searching for the type of event, method, or property you are looking for, and see if anything matches. There are plenty of useful examples available as well.
Upvotes: 4