Hi,
I have a GridView and I'm performing a bulk update for only one column with textbox. But before update I need to check the values of that textbox for entire gridview and if the values aren't changed I need to provide an alert stating "Make change to field to update".
Can anyone suggest me some options?
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
foreach (GridViewRow row in gvDetails.Rows)
{
string strID = ((Label)row.FindControl("lblID")).Text;
string strGroup = ((Label)row.FindControl("lblGrp")).Text;
string strDept = ((TextBox)row.FindControl("txtDept")).Text;
{
//my update query
}
}
}
catch (Exception ex)
{
}
}
Loading
Vincent Maverick DuranoPosted Apr 24, 2015, 3:03 PM
Hi,
Does the solution posted at aspnet forums works for you? http://forums.asp.net/t/2047093.aspx?Check+if+row+modified+in+gridview+c+
Afzaal Ahmad ZeeshanPosted Apr 22, 2015, 3:55 AM
For such a function you are required to store the initial values in your program. I am unaware of your program and its required data; Model. So I cannot tell you how you can deserialize your data into an object that can be used to check against.
In .NET framework, object class is the very forefather class to every other object. It contains a function called .Equals(object obj2). Which can be used to check whether both objects are equal or not; by reference.
Once you have created your model object, you can then continue to fill the data to a new object of the same class and then use object1.Equals(object2) or vice versa to check whether they are equal or not. Like this,
foreach (GridViewRow row in gvDetails.Rows)
{
string strID = ((Label)row.FindControl("lblID")).Text;
string strGroup = ((Label)row.FindControl("lblGrp")).Text;
string strDept = ((TextBox)row.FindControl("txtDept")).Text;
ModelClass m = new ModelClass();
m.StrID = strID;
m.StrGroup = strGroup;
m.strDept = StrDept
// Now compare
if(m.Equals(object1)) {
// they are equal
} else {
// update the data...
}
}
Using this manner you can then update the values yourself, if there is something new found for that row.