GridView get values of row 2 and 3 on linkbutton click

Adeel Mirza 121 Reputation points
2022-01-18T13:31:04.387+00:00

I have a Gridview like this.

<asp:GridView ID="gvITD" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%"
AutoGenerateColumns="false" OnRowCommand="gvITD_RowCommand">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" Width="1" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
<Columns>
<asp:BoundField DataField="Type of Return" HeaderText="Type of Return" ItemStyle-Width="120px" />
<asp:BoundField DataField="Quarter" HeaderText="Month" ItemStyle-Width="90px" />
<asp:BoundField DataField="Year" HeaderText="Year" ItemStyle-Width="70px" />
<asp:BoundField DataField="submittedDate" HeaderText="Date Of Submission" ItemStyle-Width="140px" />
<asp:BoundField DataField="Status" HeaderText="Status" ItemStyle-Width="120px" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="View Details" CommandName="ViewDetails" runat="server" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

On click of the link button I want to retrieve the value of Quarter and Year. For this I used this code in cs file.

protected void gvITD_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ViewDetails")
{
LinkButton lnkView = (LinkButton)e.CommandSource;
string lectureId = lnkView.CommandArgument;

        string strArg = e.CommandArgument.ToString();
        //int rowIndex = Convert.ToInt32(e.CommandArgument.ToString());
        //GridViewRow row = gvITD.Rows[rowIndex];

        //String Month = row.Cells[1].Text;
        //String Year = row.Cells[1].Text;

        //int month = DateTime.ParseExact(Month, "MMMM", CultureInfo.CurrentCulture).Month;

        //Response.Redirect("NodalOfficerInspection_V2.aspx");
        // write link button click event code here
    }

But I get nothing from e.CommandArgument and cannot proceed further

Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Yijing Sun-MSFT 7,096 Reputation points
    2022-01-19T02:29:38.057+00:00

    Hi @Adeel Mirza ,
    e.CommandArgument only get your linkbutton's data and you don't bind it.So,you get nothing.You could do like this:

    protected void gvITD_RowCommand(object sender, GridViewCommandEventArgs e)  
            {  
                if (e.CommandName == "ViewDetails")  
                {  
                    GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;  
                    int rowindex = row.RowIndex;  
                    string Quarter = gvITD.Rows[rowindex].Cells[1].Text.ToString();  
                    string Year= gvITD.Rows[rowindex].Cells[2].Text.ToString();  
                }  
            }  
    

    Best regards,
    Yijing Sun


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.