Reputation: 489
I have a GridView that has 7 columns. 6 of the columns have LinkButtons.
What I want to do is, get the data value that is in the 1st column and put that in a Label elsewhere on my page when one of those LinkButons is clicked.
For example the first column has a value that looks like this: 01:00 - 02:00.
Here is what I have so far: My GridView is named gvappts
I have added this property:OnRowDataBound="gvappts_DataBound"
And inside gvappts on one of my LinkButtons I have added Command and Command Arguments:<asp:LinkButton id="lbd1" runat="server" Text='<%#(Eval("Day1"))%>' CommandName="GetData" CommandArgument='<%# Container.DataItemIndex %>'/>
Because I want to Select the current row.
In my .cs I have this:
protected void gvappts_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName == "GetData")
{
//Get rowindex
int rowindex = Convert.ToInt32(e.CommandArgument);
//Get Row
GridViewRow gvr = gvappts.Rows[rowindex];
//Set the label value
lblTimeSelected.Text = gvr.Cells[0].Text;
}
}
I do not get any errors when clicking the buttons, but no value is returned. What am I overlooking?
Upvotes: 0
Views: 1479
Reputation: 76
Try this.
GridViewRow gvr = gvappts.Rows[rowindex];
Label myLabel = gvr.FindControl("MyLabel Id") as Label ;
lblTimeSelected.Text = myLabel.Text;
Upvotes: 2