Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 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 0:13am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
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
}
 

--Amit
  Permalink  
v2
Comments
vaibhav mahajan at 27-Jul-12 6:42am
   
pls read the question..!!
_Amy at 27-Jul-12 6:37am
   
Thank you Mits.
vaibhav mahajan at 27-Jul-12 6:41am
   
i know this.....bt how to check which row is clicked?...
vaibhav mahajan at 27-Jul-12 6:44am
   
i want to check which "ROW" is clicked in row_command event
vaibhav mahajan at 27-Jul-12 6:50am
   
@MIts:I tried it ...bt its not working..!!
_Amy at 27-Jul-12 6:46am
   
Add the row identity as your command argument and check that in row command..
vaibhav mahajan at 27-Jul-12 6:50am
   
@_Amy:can u pls elaborate..!!
vaibhav mahajan at 27-Jul-12 6:55am
   
i will only check the button i clicked...bt how to check which Row's button is clicked?
_Amy at 27-Jul-12 6:53am
   
GridViewRow gRow = GriwViewRow(sender);
//Now you are having Your Row
vaibhav mahajan at 27-Jul-12 7:01am
   
@_Amy :it is having errors....no overload method for GridViewRow takes 1 argument
vaibhav mahajan at 27-Jul-12 7:03am
   
dear...how can u give same id for different button...!!..pls elaborate
vaibhav mahajan at 27-Jul-12 7:04am
   
they are having different ids
vaibhav mahajan at 27-Jul-12 7:08am
   
it gives error here
 
int index = System.Convert.ToInt32(e.CommandArgument);
vaibhav mahajan at 27-Jul-12 7:13am
   
e.RowIndex does not come in row command event
vaibhav mahajan at 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;
//gr.RowIndex=

 
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>window.open('../BILLpassSYSTEM/frmDataEntry.aspx?param=" + _query_string_que + "','_blank');</script>");

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

html-------------
 

<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">


<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 at 27-Jul-12 7:25am
   
it is giving error at the
 
int index = System.Convert.ToInt32(e.CommandArgument);
 
line
vaibhav mahajan at 27-Jul-12 7:30am
   
Error:Input string was not in a correct format.
_Amy at 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 at 27-Jul-12 7:45am
   
can u pls help us out..!!
vaibhav mahajan at 27-Jul-12 7:44am
   
@mits macchi:i really thankful 4 ur intrest
vaibhav mahajan at 27-Jul-12 8:25am
   
@mits:the row_created event really done the job for me..!!
thnks a lot
cheers..!!
_Amy at 27-Jul-12 8:26am
   
Congratulations..!! :)
vaibhav mahajan at 27-Jul-12 8:32am
   
:)
Rate this: bad
good
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();
        }
    }
  Permalink  
v4
Comments
_Amy at 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
0 OriginalGriff 339
1 _Amy 310
2 Nirav Prabtani 268
3 Sergey Alexandrovich Kryukov 216
4 CHill60 170
0 OriginalGriff 8,004
1 Sergey Alexandrovich Kryukov 7,017
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 27 Jul 2012
Copyright © CodeProject, 1999-2014
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