检查是否在 gridview 中修改了行

Check if row modified in gridview

我有一个 GridView,我正在对带有文本框的一列执行批量更新。但在更新之前,我需要检查整个 gridview 的文本框的值,如果值没有改变,我需要提供一个警告,说明 "Make change to field to update".

谁能给我一些建议?

    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)
        {

        }
    }

你可以试试DataGridView.RowValidating Event and check if IsCurrentRowDirty 属性改成

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

编辑:-

以上在Winforms中有效;在Asp.net中没有这样的方法,你必须在一个对象中加载数据然后你必须验证。

你可以查看Updating Only Changed Rows Using GridView Control

使用 gridview 的 onrowdatabound 属性。在其中,使用:

 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
        }
    }

这里要做的基本事情是将原始数据加载到 DataTable 中,然后比较 GridView 行,在特定列的循环中逐一比较,在您的情况下是TextBox 列。为了比较 DataTableGridView,您可以尝试这样的操作:

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.
      }
   }
}

希望对您有所帮助。

根据对话,我提供了一些伪逻辑。您可以使用它来实现您自己的代码。

    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)
        {

        }
    }

一种更简单的方法是将 asp:HiddenField 添加到您的 ItemTemplate 并比较每一行的值。

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

现在您只需将该值与代码隐藏中每一行中的文本框值进行比较,就像这样。

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)
    {

    }
}

我希望代码是不言自明的。

对照相应项目的数据键值检查它们。

<MasterTableView DataKeyNames="Response, ...

foreach (GridDataItem item in FTReport.MasterTableView.Items)
{
  string ResponseValue = tbResponse.Text;
  if (item.GetDataKeyValue("Response").ToString() != ResponseValue)
  {
    do something;
  }
}