LINQ:Gridview正在更新具有相同值的每一行

时间:2014-03-11 06:04:33

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

我想在不使用普通Update命令的情况下更新gridview时运行一些方法。我相信我可以使用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;  }

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

我需要抓取正在编辑的行的datakey。我怎么能做到这一点?

1 个答案:

答案 0 :(得分:0)

你可以这样使用

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

但请确保datakeynames中有gridview

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