11

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

目前我有:

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

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

    int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvCurrentBanners.Rows[index];

这也不起作用,因为 CommandArgument(横幅 ID)不是行 ID。

4

6 回答 6

11
Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)

然后获取密钥或获取单元格并转换为数据控制字段。

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

Dim email As String = CType(row.Cells(2), DataControlFieldCell).Text

备注:这只适用于 Boundfields。

于 2012-05-28T11:36:15.477 回答
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;
}

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

于 2014-06-10T15:44:20.673 回答
3

试试这个..

GridViewRow gvRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
int rowIndex=gvRow.RowIndex
于 2012-12-20T06:06:34.973 回答
3

int index = Convert.ToInt32(e.CommandArgument);

它仅在您使用 Gridview 中的按钮时才有效

<Columns>   
     <asp:ButtonField ButtonType="Button" Text="Save"  CommandName="Select" /> 
</Columns>

我不确定这是否正确,但它对我有用

于 2012-12-26T03:10:25.697 回答
1
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit_Mob") {

        GridViewRow row = (GridViewRow (((ImageButton)e.CommandSource).NamingContainer);

        int RowIndex = row.RowIndex; // this find the index of row

        int varName1 = Convert.ToInt32(((Label)row.FindControl("lbl_mobile_id")).Text.ToString()); //this store the  value in varName1

        }
   }
于 2012-12-13T07:47:35.213 回答
0
if (e.CommandName == "EditDetails")
{
    mp1.Show();
    //LinkButton LnkEdit = (LinkButton)sender;
    GridViewRow gvrow = (GridViewRow)((Control)e.CommandSource).NamingContainer;
    Panel1.Visible = true;
    Uploaddocs.Visible = false;
    Label lblmobile = (Label)gvrow.FindControl("lblN_MobileNo") ;
    Label lblDeathDate = (Label)gvrow.FindControl("lblDEATHDT");
    Label lblCAUSEOFDEATH = (Label)gvrow.FindControl("lblCAUSEOFDEATH");
    Label lblPLACEOFDEATH = (Label)gvrow.FindControl("lblPLACEOFDEATH");
    // Label lblRemarks = (Label)gvrow.FindControl("lblpracticalcredits");
    Label lblBank = (Label)gvrow.FindControl("lblBank");
    Label lblBranch = (Label)gvrow.FindControl("lblBranch");
    Label lblIFSC = (Label)gvrow.FindControl("lblIFSC");
    txtMobile.Text = Convert.ToInt64(lblmobile.Text).ToString();       //May be BigInt
    txtdate.Text = lblDate.Text;
    //ddlReasons.SelectedIndex = Convert.ToInt32(lblCAUSEOFDEATH.Text);
    //ddlReasons.SelectedValue = lblCAUSEOFDEATH.Text;
    txtcausedeath.Text = lblCAUSEOFDEATH.Text;
    txtPlaceofdeath.Text = lblPLACEOFDEATH.Text;
    BindBank();
    ddlbank.SelectedItem.Text = lblBank.Text;             //Sdents
    txtAddrBank.Text = lblBranch.Text;
    txtIFSC.Text = lblIFSC.Text;
}
于 2018-08-20T07:34:24.480 回答