Delete multiple selected GridView rows using DataKeyNames (DataKeys) in ASP.Net

PRA
 
on Dec 10, 2020 10:49 PM
436 Views

Hi!

I used below code for delete multi selected rows in asp.net griview, but couldn't got id. 

<div class="bll8">
    <asp:Panel ID="Pan" runat="server" BackColor="#3CB371" Height="140px" 
            Font-Names="Palatino Linotype" Width="998px">
            <div class="bl45">
                <asp:Button ID="Delete" runat="server" onclick="Delete_Click" Text="Несткунӣ" 
                    UseSubmitBehavior="False" Width="150px" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                &nbsp;</div>
        <asp:Label ID="Label8" runat="server" Text="Label"></asp:Label>
    </asp:Panel>
    <asp:GridView ID="Grid" runat="server" AutoGenerateColumns="False"
        BackColor="White" BorderColor="Black" BorderStyle="Double" BorderWidth="2px" 
        HorizontalAlign="Center" AutoGenerateEditButton="False" 
        style="margin-left: 0px;" 
        CellPadding="3" CellSpacing="1" 
        AutoGenerateDeleteButton="False" Width="998px">
            <AlternatingRowStyle Wrap="True" />
            <Columns>
                <asp:TemplateField HeaderText="Select">  
                    <ItemTemplate>  
                         <asp:CheckBox ID="chkSelect" runat="server" />  
                    </ItemTemplate> 
                    <ControlStyle Width="30px" />
                    <FooterStyle Width="30px" Wrap="False" />
                    <HeaderStyle Width="30px" Wrap="False" />
                    <ItemStyle Width="30px" Wrap="False" />
                </asp:TemplateField>  
                <asp:TemplateField HeaderText="Код">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" AutoComplete="off" Text='<%# Bind("id") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                    </ItemTemplate>
                    <ControlStyle Width="30px" />
                    <FooterStyle Width="30px" Wrap="False" />
                    <HeaderStyle Width="30px" Wrap="False" />
                    <ItemStyle Width="30px" Wrap="False" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Ному насаб">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" AutoComplete="off" Text='<%# Bind("name") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
                    </ItemTemplate>
                    <ControlStyle Width="160px" />
                    <FooterStyle Width="160px" Wrap="False" />
                    <HeaderStyle Width="160px" Wrap="False" />
                    <ItemStyle Width="160px" Wrap="False" />
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
            <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
            <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
            <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#594B9C" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#33276A" />
        </asp:GridView>
    </div>

 

	    protected void Delete_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in Grid.Rows)
            {
                if ((row.FindControl("chkSelect") as CheckBox).Checked)
                {
                    delpeople(Grid.DataKeys[row.RowIndex].Values[0].ToString());
                }
            }
            BindData();            
        }

        private void BindData()
        {
            Grid.DataSource = peoplesName();
            Grid.DataBind();
        }	

	    public void delpeople(string kod)
        {
            SqlConnection con = new SqlConnection(UserData);
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = "delete from karta where kod_kart ='" + kod + "'";
            con.Open();
            try
            {
                cmd.ExecuteReader();
            }
            catch { };
            con.Close();
        }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Dec 10, 2020 10:53 PM

Hi PRA,

You did not set the DataKeyNames property in GridView.

Refer below article for more details on DataKeys.

Understanding ASP.Net GridView DataKeyNames (DataKeys) with examples