Get Value of DataGrid Using LinkButton C#

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?
OteroJCAsked:
Who is Participating?
 
jmwheelerConnect With a Mentor Commented:
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">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton CommandArgument='<%# Eval("ColumnNameFromData") %>' Text="Test" OnClick="LinkButton_Click" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
 
 
protected void LinkButton_Click(object sender, EventArgs e)
    {
        LinkButton b = (LinkButton)sender;
        string x = b.CommandArgument;
    }

Open in new window

0
 
OteroJCAuthor Commented:
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.
0
 
jmwheelerCommented:
No problem
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.