2013-03-12 97 views
0

我正在尋找ASP.net可編輯網格視圖的代碼。
在一些網站我得到了編碼。但它有一個額外的按鈕,如編輯或更新。點擊該按鈕時,網格變爲可編輯。你可以看到它here

但我希望網格在單擊網格單元格時進行編輯。我不需要額外的按鈕來點擊然後編輯。

那麼如何才能使網格可編輯只需點擊它?ASP.net中的可編輯網格

回答

0

您需要使用GridViewRowDataBound事件來實現此目的。我寫了一個你如何做的例子。改變你的要求。

<asp:GridView runat="server" ID="gv" onrowdatabound="gv_RowDataBound" 
       onrowediting="gv_RowEditing"> 
</asp:GridView> 

代碼隱藏看起來就像這樣:

private void LoadGrid() 
{ 
    gv.DataSource = YourDataSource; 
    gv.DataBind(); 
} 

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gv, "Edit$" + e.Row.RowIndex); 

    } 
} 

protected void gv_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
    gv.EditIndex = e.NewEditIndex; 
    this.LoadGrid(); 
}