我只是找到了一個解決方案對他們來說,這可能感興趣的:
首先,你應該包括fllwg線BEFOREthe數據綁定:
gv.RowDataBound += gv_RowDataBound;
gv.RowCommand += gv_RowCommand;
然後定義的RowDataBound插入的LinkButton:
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);
}
}
希望調用命令,這是對別人有幫助!
試試這個:http://csharp.net-informations.com/datagridview/csharp-datagridview-button.htm – MrFox