Get Value of DataGrid Using LinkButton C#

Posted on 2009-02-19
Last Modified: 2012-06-21
I have a data grid grdSchedule that has a template column in it with a link button. When a user clicks the link button I want to retrieve the value of the fifth column and display it in a a separate control. I'm using Visual Studio 2008 and C#. How do I get the value of the fifth column for the row on which the user has clicked the link button?
Question by:OteroJC
    LVL 11

    Accepted Solution

    Rather than try and find the value from the fifth column, you should add the value of the 5th column to the commandargument property of the link button when binding the data.
    <asp:GridView ID="grdSchedule" runat="server">
                        <asp:LinkButton CommandArgument='<%# Eval("ColumnNameFromData") %>' Text="Test" OnClick="LinkButton_Click" runat="server" />
    protected void LinkButton_Click(object sender, EventArgs e)
            LinkButton b = (LinkButton)sender;
            string x = b.CommandArgument;

    Open in new window


    Author Closing Comment

    Thank you so much. I was over complicating my solution and succeeding only at giving myself a big giant headache. This is the neatest (cleanest) solution I have seen in all my searching.
    LVL 11

    Expert Comment

    No problem

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B. provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
    Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now