Get cell value in C # Gridview when button is clicked on row

I have a gridviewMain. Whenever I press the CLOSE button for line 1, I want to get the value aa. Click Close on line 2 to get the bb value. Any ideas.

ABC 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> 
+4
source share
4 answers

Assuming you are using BoundFields for the first three columns and want to handle the LinkButton -click event (instead of the RowCommand 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 use TemplateFields and the aa value is in the label (e.g. LblValue ):

 Label lblValue = (Label) row.FindControl("LblValue"); // lblValue.Text = "aa" 
+12
source

Use data keys for this. Much simpler:

 <asp:GridView ID="GridView1" runat="server" DataKeyNames="SomeValue, AnotherValue" ... > 

And then in encoding, just get the keys associated with the string:

 var rowIndex = 0; var someValue = GridView1.DataKeys[rowIndex]["SomeValue"] as string; 
+3
source

You need to assign CommandName LinkButton column in the GridView markup. From there, you will also need to hook up the OnRowCommand event to process your Close command.

The following is an example of using the Add command on a GridView :

 Markup: <asp:gridview id="ContactsGridView" datasourceid="ContactsSource" allowpaging="true" autogeneratecolumns="false" onrowcommand="ContactsGridView_RowCommand" runat="server"> <columns> <asp:buttonfield buttontype="Link" commandname="Add" text="Add"/> <asp:boundfield datafield="ContactID" headertext="Contact ID"/> <asp:boundfield datafield="FirstName" headertext="First Name"/> <asp:boundfield datafield="LastName" headertext="Last Name"/> </columns> </asp:gridview> Code-Behind: void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e) { // If multiple buttons are used in a GridView control, use the // CommandName property to determine which button was clicked. if(e.CommandName=="Add") { // Convert the row index stored in the CommandArgument // property to an Integer. int index = Convert.ToInt32(e.CommandArgument); // Retrieve the row that contains the button clicked // by the user from the Rows collection. GridViewRow row = ContactsGridView.Rows[index]; // Create a new ListItem object for the contact in the row. ListItem item = new ListItem(); item.Text = Server.HtmlDecode(row.Cells[2].Text) + " " + Server.HtmlDecode(row.Cells[3].Text); // If the contact is not already in the ListBox, add the ListItem // object to the Items collection of the ListBox control. if (!ContactsListBox.Items.Contains(item)) { ContactsListBox.Items.Add(item); } } } 
0
source

You can do it

 <asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" onrowcommand="grid_RowCommand" > <Columns> <asp:TemplateField > <ItemTemplate> <asp:TextBox ID="txt" runat="server" Text='<%#Eval("xxx")%>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField > <ItemTemplate> <asp:LinkButton ID="lnk" CommandArgument=<%# Container.DataItemIndex + 1 %> CommandName="arg">Click</asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> 

code

  protected void grid_RowCommand(object sender, GridViewCommandEventArgs e) { int rowindex = int.Parse(e.CommandArgument.ToString()); ((TextBox)(grid.Rows[rowindex].FindControl("txtgvunit"))).Text } 

Link http://dotnetinbox.blogspot.in/2014/02/get-gridview-row-data-in-c.html

0
source

Source: https://habr.com/ru/post/1488693/


All Articles