Get highlighted row Gridview

I have a Gridview.I checked for each row. Having selected the checkbox and clicking the delete button, I should get the entire selected row of this gridview in the code. Please help me...

<asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" style="margin-left: 58px; margin-top: 13px" > <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> <Columns> <asp:TemplateField> <EditItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </EditItemTemplate> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:Button ID="Delete" runat="server" Text="Button" runat="server" onclick="Delete_Click" /> 

codebehind

 protected void Button1_Click(object sender, EventArgs e) { string valueForDB = DatabaseList.SelectedValue; Data obj = new Data(); if (valueForDB == "virtualworkplace") { obj.getDocforVirtualwrkspace(valueForDB); GridView1.DataSource = obj.getDocforVirtualwrkspace(valueForDB); GridView1.DataBind(); } What to write here??? protected void Delete_Click(object sender, EventArgs e) { } 
+4
source share
2 answers

try it

  private void DeleteRows() { foreach (GridViewRow row in gridView1.Rows) { HtmlInputCheckBox chk = (HtmlInputCheckBox) row.Cells[0].FindControl("selectedrowchk"); if (chk != null && chk.Checked) { string id = gridView1.DataKeys[row.RowIndex].Value.ToString(); // get the record ID of this row deleteRecord(id); } } //RefreshGrid(); } 

This code assumes that the DataKeyNames GridView property will store the primary key of each record. It is also assumed that you check the box in the first column

+3
source

to set datakeys, use the datakeynames property from gridview

  <asp:GridView AllowSorting="true" AutoGenerateColumns="false" AllowPaging="true" PageSize="10" DataKeyNames="MembershipNo" ID="grdvw_showdetails" runat="server" CellPadding="4" 
+2
source

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


All Articles