在RowCommand中获取GridView Cell的值

Dar*_*den 9 c# vb.net asp.net gridview

我需要从RowCommand事件中获取单元格的值,但该值不在GridView的PrimaryKeyNames参数中.

目前我有:

if (e.CommandName == "DeleteBanner")
        {
            GridViewRow row = gvCurrentPubBanner.SelectedRow;
            string BannerName = row.Cells[1].Text;
Run Code Online (Sandbox Code Playgroud)

这不起作用(索引超出范围错误),我也尝试过:

    int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvCurrentBanners.Rows[index];
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为CommandArgument(标题ID)不是行ID.

Rom*_*ain 10

Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
Run Code Online (Sandbox Code Playgroud)

然后获取密钥或获取单元格并转换为数据控制域.

Dim id As Guid = GridView1.DataKeys(row.RowIndex).Value

Dim email As String = CType(row.Cells(2), DataControlFieldCell).Text
Run Code Online (Sandbox Code Playgroud)

备注:这仅适用于Boundfields.


Chư*_*iết 6

@Romil:你的 C# 代码:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
    int id = (int)GridView1.DataKeys[row.RowIndex].Value;
}
Run Code Online (Sandbox Code Playgroud)

(LinkBut​​ton 是模板字段中的按钮)。