【问题标题】:LINQ: Gridview is updating every row with same valuesLINQ:Gridview 正在使用相同的值更新每一行
【发布时间】:2014-03-11 06:04:32
【问题描述】:

我想在不使用普通更新命令的情况下更新网格视图时运行一些方法。不过,我相信我可以使用 RowUpdating 来做到这一点。

 protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        int selectedRowIndex = e.RowIndex;

        TextBox txtID = (TextBox)GridView2.Rows[e.RowIndex].FindControl("TextBox13");
        TextBox txtDate1 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtDate1");
        TextBox txtDate2 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtDate2");
        Label duration = (Label)GridView2.Rows[e.RowIndex].FindControl("Label13");
        Label diff = (Label)GridView2.Rows[e.RowIndex].FindControl("Label14");
        Label remainder = (Label)GridView2.Rows[e.RowIndex].FindControl("Label15");
        Label expiry = (Label)GridView2.Rows[e.RowIndex].FindControl("Label17");
        DropDownList ddlName = (DropDownList)GridView2.Rows[e.RowIndex].FindControl("ddlName");
        DropDownList ddlType = (DropDownList)GridView2.Rows[e.RowIndex].FindControl("ddlType");
        DropDownList ddlLocation = (DropDownList)GridView2.Rows[e.RowIndex].FindControl("ddlLocation");
        DropDownList ddlFee = (DropDownList)GridView2.Rows[e.RowIndex].FindControl("ddlFee");

         //dc.updateArchive(Variables goes here);
         GridView2.DataBind();
         GridView2.EditIndex = -1;  }

搞砸之后,我能够从所选行(我单击“编辑”的行)中读取值,但这里发生的情况是,每一行都使用我在“编辑”行中输入的相同值进行更新模式(选择?)。

我需要获取正在编辑的行的数据键。我怎样才能做到这一点?

【问题讨论】:

    标签: c# asp.net sql-server linq gridview


    【解决方案1】:

    你可以这样使用

    var key=GridView2.DataKeys[selectedRowIndex].Value;
    

    但请确保您的gridview 中有datakeynames

    datakeynames="LocationID"
    //name of you actual primary key field in you select data source.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-24
      • 2017-06-22
      • 2023-03-24
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 1970-01-01
      相关资源
      最近更新 更多