10

I need to get the value of a cell from the RowCommand event, but the value is not in the PrimaryKeyNames parameter of the GridView.

Currently I have:

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

This doesn't work (index out of range error), I've also tried:

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

This doesn't work either as the CommandArgument (the banner ID) is not the row ID.

Romil Kumar Jain
  • 20,239
  • 9
  • 63
  • 92
DarrylGodden
  • 1,526
  • 5
  • 25
  • 44

6 Answers6

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

Then get the key or get the cell and cast to a datacontrolfield.

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

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

Remark: this only works with Boundfields.

Romil Kumar Jain
  • 20,239
  • 9
  • 63
  • 92
6

@Romil: your code in C#:

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

(LinkButton is button in template field).

Chưa biết
  • 919
  • 8
  • 6
3

Try out this one..

GridViewRow gvRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
int rowIndex=gvRow.RowIndex
Bijoy K Jose
  • 1,342
  • 3
  • 15
  • 26
  • I dont see how that could work, as you're creating a new GridviewRow, not referencing the existing one in the gridview – Fandango68 Feb 06 '16 at 21:34
3

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

It only work when you use your button in Gridview is

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

I not sure this is correct but it work for me

Wolf
  • 6,361
  • 2
  • 28
  • 25
  • 3
    This is the correct solution, as documented on the [MSDN page on `RowCommand`](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand%28v=vs.110%29.aspx): _The ButtonField class automatically populates the CommandArgument property with the appropriate index value._ – marapet Feb 10 '14 at 09:10
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

        }
   }
Kisan Patel
  • 101
  • 3
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;
}
Druid
  • 6,423
  • 4
  • 41
  • 56