Get Current row from Gridview in ASP.NET

I have a Gridview with delete and edit buttons looks like

<asp:GridView ID="grdTrackedItems" runat="server" AutoGenerateColumns="False" Width="330px"
            BorderStyle="None" OnRowDataBound="OnRowDataBoundTrackedItems" OnRowDeleting="OnRowDeletingTrackedItems">
            <Columns>
              <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                  <asp:Label ID="lblItem" runat="server" Text='<%# Eval("Item") %>' />
                </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField>
                <ItemTemplate>
                  <asp:ImageButton ID="imgDelete" runat="server" ImageUrl="~/Resources/Images/Delete.png"
                    Height="12" Width="12" ToolTip="Delete" CommandName="Delete" />
                </ItemTemplate>
                <ItemStyle Width="22px" />
              </asp:TemplateField>
              <asp:TemplateField>
                <ItemTemplate>
                  <asp:ImageButton ID="imgEdit" runat="server" ImageUrl="~/Resources/Images/Edit.png"
                    Height="12" Width="12" ToolTip="Edit" />
                </ItemTemplate>
                <ItemStyle Width="22px" />
              </asp:TemplateField>
            </Columns>
          </asp:GridView>

I have a text box and Save button on bottom of the page. I want to display the item name in the textbox when I click on the edit button from gridview. How can I do this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Provide a handler for the RowEditing event, and then update your textbox in that handler.

<asp:GridView ID="grdTrackedItems" runat="server" 
             AutoGenerateColumns="False" Width="330px"
            ...
             OnRowEditing="EditRecord">
            <Columns>


protected void EditRecord(object sender, GridViewEditEventArgs e)
{
    Label lbl = (Label)grdTrackedItems.Rows[e.NewEditIndex].Cells[0].FindControl("lblItem");


    MyTextBox.Text = lbl.text;
}

Method 2

inside the edit button click handler you can access the current row as below:

protected void Button1_Click1(object sender, EventArgs e)
{
    GridViewRow row = ((ImageButton)sender).Parent.Parent as GridViewRow;
    // Do some thing with this row
}

Method 3

I got the solution from river-blue’s answer.

Add DataKeyNames=”Item” to Gridview
and inside the edit button click handler

     protected void OnClickEdit( object sender, EventArgs e )
    {
        GridViewRow row = ( (ImageButton) sender ).Parent.Parent as GridViewRow;
        txtName.Text =  grdTrackedItems.DataKeys[row.RowIndex].Value.ToString();
    }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x