Check if row has changed in gridview

I have a GridView, and I am performing a bulk update for only one column with a text field. But before updating, I need to check the values ​​of this text field for the entire gridview and, if the values ​​are not changed, I need to provide a warning with the message "Make a change in the field for updating".

Can someone 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 strValue = ((TextBox)row.FindControl("txtValue")).Text; { //my update query } } } catch (Exception ex) { } } 
+5
source share
5 answers

It will be much easier to add asp:HiddenField to the ItemTemplate and compare the value for each row.

 <asp:HiddenField ID="" runat="server" Value='<%# Eval("blah") %>'></asp:HiddenField> 

Now you need to compare this value with the value of the text field in each line of code in this way.

 protected void btnUpdate_Click(object sender, EventArgs e) { try { var isAnyRowUpdated = false; foreach (GridViewRow row in gvDetails.Rows) { string strID = ((Label)row.FindControl("lblID")).Text; string strGroup = ((Label)row.FindControl("lblGrp")).Text; string strValue = ((TextBox)row.FindControl("txtValue")).Text; string strOldValue = ((HiddenField)row.FindControl("hdnOldValue")).Value; if (strValue != strOldValue) { isAnyRowUpdated = true; //update procedure here. } } //now check if the flag is still false //that means no rows are changed if(!isAnyRowUpdated) { //alert no rows are updated } } catch (Exception ex) { } } 

I hope the code is self explanatory.

+4
source

You can try to use the DataGridView.RowValidating Event and check if the <IsCurrentRowDirty Property has changed

IsCurrentRowDirty Property Gets a value indicating whether the current row has uncommitted changes.

EDIT: -

The above works in Winforms; there is no such method in Asp.net, you need to load the data into the object, and then you have to check.

You can check Update only changed rows using GridView Control

+2
source

use the onrowdatabound gridview property. Inside it use:

  protected void GridLocation_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit) { // here you can check your textbox values } } 
0
source

The main task is to load the source data into a DataTable , and then compare the rows of the GridView , one by one in a loop for that particular column, which in your case is a TextBox column. To compare a DataTable with a GridView you can try something like this:

 foreach (GridViewRow row in Grd.Rows) { TextBox txtAmt = (TextBox)row.FindControl("txtAmount"); string Id = Grd.DataKeys[row.RowIndex].Value.ToString(); for (int i = 0; i < dt.Rows.Count; i++) { if (Id == dt.Rows[i]["ID"].ToString()) { //do your logic here. } } } 

Hope this helps.

0
source

In accordance with the conversation, I presented some pseudo-logic. You can implement your own code using this.

  protected void btnUpdate_Click(object sender, EventArgs e) { try { DataTable dt = LoadData(); //Load the data from DB EnumerableRowCollection<DataRow> enumerableDt = dt.AsEnumerable(); foreach (GridViewRow row in gvDetails.Rows) { string strID = ((Label)row.FindControl("lblID")).Text; string strGroup = ((Label)row.FindControl("lblGrp")).Text; string strValue = ((TextBox)row.FindControl("txtValue")).Text; DataRow dr = enumerableDt.Where(x => x.Field<string>("ID") == strID).FirstOrDefault(); //Change your condition accordingly if (dr["Value"].ToString().ToUpper() != strValue.Trim().ToUpper()) //Change your condition here { //Do your updated data logic here } else { //Do your not updated data logic here } } } catch (Exception ex) { } } 
0
source

All Articles