2012-11-09 136 views
0

我使用AJAX Control Toolkit來創建Tabpanels。每個面板都按照以下代碼填充gridview。如何在動態創建的Gridview中創建按鈕?

現在,我想每行添加一個按鈕。當它被點擊時,它應該作爲該行單元格的參數之一傳遞,但是由於Gridview是動態創建的,我不知道如何。有小費嗎?

foreach (DataTable dt in DataSet1.Tables) 
{ 
    GridView gv = new GridView(); 
    var thepanel = new AjaxControlToolkit.TabPanel(); 
    gv.DataSource = dt; 
    gv.DataBind(); 
    thepanel.Controls.Add(gv); 
    TabContainer.Controls.Add(thepanel); 
} 
+0

試試這個:http://csharp.net-informations.com/datagridview/csharp-datagridview-button.htm – MrFox

回答

0

我只是找到了一個解決方案對他們來說,這可能感興趣的:

首先,你應該包括fllwg線BEFOREthe數據綁定:

gv.RowDataBound += gv_RowDataBound; 
gv.RowCommand += gv_RowCommand; 

然後定義的RowDataBound插入的LinkBut​​ton:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 

      LinkButton butIgnorar = new LinkButton() 
      { 
       CommandName = "Ignorar", 
       ID = "butIgnorar", 
       Text = "Ignorar", 
       //optional: passes contents of cell 1 as parameter 
       CommandArgument = e.Row.Cells[1].Text.ToString() 
      }; 
      //Optional: to include some javascript cofirmation on the action 
      butIgnorar.Attributes.Add("onClick", "javascript:return confirm('Are you sure you want to ignore?');"); 
      TableCell tc = new TableCell(); 
      tc.Controls.Add(butIgnorar); 
      e.Row.Cells.Add(tc); 
     } 
    } 

最後,從RowCommand

protected void gv_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 

     string currentCommand = e.CommandName; 
     string parameter= e.CommandArgument.ToString(); 

     if (currentCommand.Equals("Ignorar")) 
     { 
      yourMethodName(parameter); 
     } 
    } 

希望調用命令,這是對別人有幫助!

0

您可以添加一個選擇按鈕,網格如下:

Gridview1.AutoGenerateSelectButton=true;