簡體   English   中英

LINQ:Gridview用相同的值更新每一行

[英]LINQ: Gridview is updating every row with same values

我想在更新gridview時運行某些方法,而不使用常規的Update命令。 而且我相信我可以使用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;  }

弄亂之后,我能夠從所選行(單擊“編輯”的行)中讀取值,但是這里發生的是,每行都使用與在“編輯”模式(“選擇” ?)。

我需要獲取正在編輯的行的數據鍵。 我該怎么做?

你可以這樣使用

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

但是,請確保您的gridviewdatakeynames

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM