简体   繁体   中英

Get Cell value from selected row gridview

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?

Try this.

 GridViewRow gvr = gvappts.Rows[rowindex];
 Label myLabel  = gvr.FindControl("MyLabel Id") as Label ;
 lblTimeSelected.Text = myLabel.Text;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM