Hi nauna,
Check this example. Now please take its refrence.
HTML
<center>
<div>
<asp:TextBox runat="server" ID="txtCountryID" />
<asp:Button Text="Delete" runat="server" ID="btnDelete" OnClick="Delete" />
<br />
<br />
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Customer ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("Customerid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country ID">
<ItemTemplate>
<asp:Label ID="lblCountryID" runat="server" Text='<%# Eval("CountryID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</center>
Namespace
using CustomersModel;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
TestEntities1 entities = new TestEntities1();
this.gvCustomers.DataSource = entities.CustomerTests.ToList();
this.gvCustomers.DataBind();
}
protected void Delete(object sender, EventArgs e)
{
int countryID = Convert.ToInt32(txtCountryID.Text);
using (TestEntities1 entities = new TestEntities1())
{
entities.CustomerTests.Where(x => x.CountryID == countryID).ToList().ForEach(entities.CustomerTests.DeleteObject);
entities.SaveChanges();
}
this.BindGrid();
}
Screenshot