Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Cell Value in C# Gridview when Button clicked on row

I have a gridviewMain. Whenever I click the CLOSE linkbutton for row 1 I want to get the value aa. Click Close on row 2 get the value of bb. Any ideas.

A    B   C  
aa  xx   3  CLOSE 
bb  yy   4  CLOSE
cc  zz   5  CLOSE

aspx

 <asp:BoundField DataField="afield" HeaderText="A" 
     SortExpression="afield" >
    <ItemStyle HorizontalAlign="Center" />
 </asp:BoundField>
 <asp:BoundField DataField="bfield" HeaderText="B" SortExpression="cfield" >
     <ItemStyle HorizontalAlign="Center" />
  </asp:BoundField>
  <asp:BoundField DataField="cfield" HeaderText="C" SortExpression="cfield" >
     <ItemStyle HorizontalAlign="Center" />
   </asp:BoundField>
 <asp:TemplateField ShowHeader="False">

       <ItemTemplate>
          <asp:LinkButton ID="lbClose" runat="server" CausesValidation="False"  CommandName="CloseClicked" Text="Close"></asp:LinkButton>

        </ItemTemplate>

                    </asp:TemplateField>
like image 729
Apollo Avatar asked Dec 01 '22 19:12

Apollo


1 Answers

Asssuming that you are using BoundFields for the first three columns and you want to handle the LinkButton-click event (instead of the e.g. RowCommand of the GridView):

protected void CloseLinkClicked(Object sender, EventArgs e)
{
    var closeLink = (Control) sender;
    GridViewRow row = (GridViewRow) closeLink.NamingContainer;
    string firstCellText = row.Cells[0].Text; // here we are
}

If you are using TemplateFields and the value aa is in a Label (e.g. LblValue):

Label lblValue = (Label) row.FindControl("LblValue"); // lblValue.Text = "aa"
like image 83
Tim Schmelter Avatar answered Dec 11 '22 10:12

Tim Schmelter