Home > Forum > ASP.NET > Datagridview deletion of row in asp.net

Datagridview deletion of row in asp.net

by Thamil   on Sep 26, 2014   Category: ASP.NET  |  Views: 1492    |  Points: 5   |  Gold 
  Reply

Sorry for this simple question.
How to delete a row based upon that selected row column value ?

For eg:
Month Feename Amount Fine
Sep Tuition 500 0
Oct Tuition 500 150
Nov Tuition 500 0

So here based i have to check for themonth where fine is dere.
If i select 2nd row to delete , then here fine is 150 .So it should get deleted and textbox for fine should become ).

Hope you understood



Question posted on behalf of Namrata.

Bookmark and Share:
 
  User Reply  | Ask a question  |   Reply 
  Re :Datagridview deletion of row in asp.net   
by Thamil
on Sep 26 2014 4:39AM
Points : 10
Gold 
Hi Namrata,

I have created sample page as per your requirement, find the below code and let me know if you have any question.

ASPX page design view

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GrdViewDelete._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridVwDelete" runat="server" AutoGenerateColumns="False" Font-Names="Verdana"
PageSize="5" Width="75%" BorderColor="#CCCCCC"
BorderStyle="Solid" BorderWidth="1px" OnRowCommand="GridVwDelete_RowCommand" DataKeyNames="Id">
<AlternatingRowStyle BackColor="#BFE4FF" />
<PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
<HeaderStyle Height="30px" BackColor="#6DC2FF" Font-Size="15px" BorderColor="#CCCCCC"
BorderStyle="Solid" BorderWidth="1px" />
<RowStyle Height="20px" Font-Size="13px" BorderColor="#CCCCCC" BorderStyle="Solid"
BorderWidth="1px" />
<Columns>
<asp:BoundField DataField="Id" HeaderText="ID" />
<asp:BoundField DataField="Month" HeaderText="Month" />
<asp:BoundField DataField="FeeName" HeaderText="FeeName" />
<asp:BoundField DataField="Amount" HeaderText="Amount" />
<asp:BoundField DataField="Fine" HeaderText="Fine" />
<asp:TemplateField HeaderText="Delet">
<ItemTemplate>
<span onclick="return confirm('Are you sure want to delete?')">
<asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
</span>
<asp:LinkButton ID="lnkCancel" runat="server" CommandName="Delete" Text="Cancel"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

Code behind view
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Getdata();
}
}
private void Getdata()
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
DataSet dsEmployee = new DataSet();
string strSelectCmd = "SELECT [Id],[Month],[FeeName],[Amount],[Fine] FROM [tblTutioFeeDetail]";
SqlDataAdapter da = new SqlDataAdapter(strSelectCmd, conn);
da.Fill(dsEmployee, "TuitionDt");
DataView dvEmp = dsEmployee.Tables["TuitionDt"].DefaultView;
GridVwDelete.DataSource = dvEmp;
GridVwDelete.DataBind();
}
}
protected void GridVwDelete_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Delete"))
{
GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
int ID = Convert.ToInt32(GridVwDelete.DataKeys[row.RowIndex].Values[0].ToString());
Deletedata(ID);
Getdata();
}
}

private void Deletedata(int id)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
conn.Open();
string Strcmd = "DELETE FROM tblTutioFeeDetail WHERE Id=" + id;
SqlCommand cmd = new SqlCommand(Strcmd,conn);
cmd.ExecuteNonQuery();
}
}

Recent Post

Latest Posts