Click here to Skip to main content
12,755,986 members (35,314 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#2.0 C# ASP.NET Beginner , +
Hello Everyone..!!
I have a data grid in which first two columns are item templates containing buttons.

I have a row_command event where i check the command name property of
particular button.

i want to check which row button is checked

i have used the following code.
but it is not working.

if (e.CommandName=="rate")
            int index = Convert.ToInt32(e.CommandArgument);

            GridViewRow gr = grdOTIF.Rows[index];

Kindly help me with the same.
Posted 27-Jul-12 1:13am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try this:
HTML:(In the item template of your gridview)
<asp:linkbutton id="lnkEdit" runat="server" text="Edit" commandname="Edit"/>
 <asp:linkbutton id="lnkEdit" runat="server" text="Edit" commandname="Delete"/>

Code Behind:

if (e.CommandName=="Edit")
    //do edit operation
else if (e.CommandName=="Delete")
    //do the deletion

_Amy 27-Jul-12 6:37am
Thank you Mits.
vaibhav mahajan 27-Jul-12 6:41am
i know how to check which row is clicked?...
vaibhav mahajan 27-Jul-12 6:42am
pls read the question..!!
vaibhav mahajan 27-Jul-12 6:44am
i want to check which "ROW" is clicked in row_command event
_Amy 27-Jul-12 6:46am
Add the row identity as your command argument and check that in row command..
vaibhav mahajan 27-Jul-12 6:50am
@MIts:I tried it its not working..!!
vaibhav mahajan 27-Jul-12 6:50am
@_Amy:can u pls elaborate..!!
_Amy 27-Jul-12 6:53am
GridViewRow gRow = GriwViewRow(sender);
//Now you are having Your Row
vaibhav mahajan 27-Jul-12 6:55am
i will only check the button i how to check which Row's button is clicked?
vaibhav mahajan 27-Jul-12 7:01am
@_Amy :it is having overload method for GridViewRow takes 1 argument
vaibhav mahajan 27-Jul-12 7:03am can u give same id for different button...!!..pls elaborate
vaibhav mahajan 27-Jul-12 7:04am
they are having different ids
vaibhav mahajan 27-Jul-12 7:08am
it gives error here

int index = System.Convert.ToInt32(e.CommandArgument);
vaibhav mahajan 27-Jul-12 7:13am
e.RowIndex does not come in row command event
vaibhav mahajan 27-Jul-12 7:17am
protected void grdOTIF_RowCommand(object sender, GridViewCommandEventArgs e)

if (e.CommandName=="rate")
int index = System.Convert.ToInt32(e.CommandArgument);

GridViewRow gr = (GridViewRow)grdOTIF.SelectedRow;

string _trans_code = "";
string _trans_name = "";
string _from_loc = "";
string _to_loc = "";
string _load_type = "";

string _queryString = "";

_trans_code = gr.Cells[2].Text.ToString();
_trans_name = gr.Cells[3].Text.ToString();
_from_loc = gr.Cells[4].Text.ToString();
_to_loc = gr.Cells[5].Text.ToString();
_load_type = gr.Cells[6].Text.ToString();

_queryString = _trans_code + ",";
_queryString = _queryString +_trans_name +",";
_queryString = _queryString + _from_loc + ",";
_queryString = _queryString + _to_loc + ",";
_queryString = _queryString + _load_type;

//Response.Write("<script>'../BILLpassSYSTEM/frmDataEntry.aspx?param=" + _query_string_que + "','_blank');</script>");

if (e.CommandName == "days")
foreach (GridViewRow gr in grdOTIF.Rows)



<asp:Panel ID="panelOTIF" runat="server" Width="1000px">
<asp:GridView ID="grdOTIF" runat="server" CssClass="MyGridView" BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"
Height="88px" OnRowCommand="grdOTIF_RowCommand" OnRowEditing="grdOTIF_RowEditing" AutoGenerateColumns="False">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" Font-Underline="True" ForeColor="White" />
<asp:TemplateField HeaderText="RATE">
<asp:Button ID="btnrate" runat="server" Text="Add Rate" CommandName="rate" />

<asp:TemplateField HeaderText="DAYS">
<asp:Button ID="btndays" runat="server" Text="Add Days" CommandName="days" />

<asp:BoundField DataField="TRANS_CODE" HeaderText="TRANS CODE" />
<asp:BoundField DataField="TRANS_NAME" HeaderText="TRANS NAME" />
<asp:BoundField DataField="FROM_LOC" HeaderText="FROM_LOC" />
<asp:BoundField DataField="TO_LOC" HeaderText="TO_LOC" />
<asp:BoundField DataField="LOAD_TYPE" HeaderText="LOAD_TYPE" />
<asp:BoundField DataField="RATE" HeaderText="RATE" />
<asp:BoundField DataField="DAYS" HeaderText="DAYS" />

vaibhav mahajan 27-Jul-12 7:25am
it is giving error at the

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

vaibhav mahajan 27-Jul-12 7:30am
Error:Input string was not in a correct format.
_Amy 27-Jul-12 7:37am
I never thought about a small question having long discussion. It could have been solved using Google[^] also.
vaibhav mahajan 27-Jul-12 7:44am
@mits macchi:i really thankful 4 ur intrest
vaibhav mahajan 27-Jul-12 7:45am
can u pls help us out..!!
vaibhav mahajan 27-Jul-12 8:25am
@mits:the row_created event really done the job for me..!!
thnks a lot
_Amy 27-Jul-12 8:26am
Congratulations..!! :)
vaibhav mahajan 27-Jul-12 8:32am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Change as Below :

<asp:linkbutton id="lnkEdit" runat="server"text="Edit" commandname="Edit"/>
<asp:linkbutton id="lnkdelete" runat="server" text="Edit" commandname="Delete"/>

Code Behind:

if (e.CommandName.equals("Edit"))
       int index = System.Convert.ToInt32(e.CommandArgument);
else if(e.commandname.equals("Delete"))
         int index = System.Convert.ToInt32(e.CommandArgument);

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            var LinkButton2 = (Button)e.Row.FindControl("btnrate");
            LinkButton2.CommandArgument = e.Row.RowIndex.ToString();
_Amy 27-Jul-12 7:46am
For your interest in this question. +5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 27 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100