CRUD in GridView

Manimaddu
Posted by Manimaddu under ASP.NET category on | Points: 40 | Views : 704
Hi All...

Create a table name as Grid
CREATE TABLE Grid
(
Sid INT PRIMARY KEY,
SName VARCHAR(20),
ExamType VARCHAR(20),
Subject1 INT,
Subject2 INT,
Subject3 INT
Total INT
)


GridPage.aspx:



<form id="form1" runat="server" dir="rtl">
<div align="center" dir="ltr">
<asp:GridView ID="TempGrid" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="TempGrid_RowCancelingEdit"
OnRowDeleting="TempGrid_RowDeleting" OnRowEditing="TempGrid_RowEditing" OnRowUpdating="TempGrid_RowUpdating" AllowSorting="True" BackColor="#CCFF99" BorderColor="#FF0066" BorderStyle="Solid"
BorderWidth="1px" Caption="Student List" CaptionAlign="Top" ForeColor="Fuchsia">
<Columns>
<asp:TemplateField HeaderText="Student ID">
<ItemTemplate><asp:Label ID="lblSid" runat="server" Text='<% #Eval("Sid") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name">
<ItemTemplate>
<asp:Label ID="lblsname" runat="server" Text='<% #Eval("Sname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtSname" runat="server" Text='<% #Eval("Sname") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ExamType">
<ItemTemplate>
<asp:Label ID="lblExamType" runat="server" Text='<% #Eval("ExamType") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtExamType" runat="server" Text='<% #Eval("ExamType") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subject 1">
<ItemTemplate>
<asp:Label ID="lblsub1" runat="server" Text='<% #Eval("Subject1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtSub1" runat="server" Text='<% #Eval("Subject1") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subject 2">
<ItemTemplate>
<asp:Label ID="lblSub2" runat="server" Text='<% #Eval("Subject2") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtSub2" runat="server" Text='<% #Eval("Subject2") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subject 3">
<ItemTemplate>
<asp:Label ID="lblsub3" runat="server" Text='<% #Eval("Subject3") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtSub3" runat="server" Text='<% #Eval("Subject3") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Text='<% #Eval("Total") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTotal" runat="server" Text='<% #Eval("Total") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>|
<asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>|
<asp:LinkButton ID="lnkcancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle ForeColor="#FF0066" />
</asp:GridView>
</div>
<asp:Button ID="btnLogout" runat="server" PostBackUrl="~/LoginForm.aspx" Text="Logout" Width="86px" />
<p align="left">
<asp:LinkButton ID="lnkCreateNew" runat="server" PostBackUrl="NewDataPage.aspx">Create New</asp:LinkButton>
</p>
</form>


GridPage.aspx.cs:


using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace CURD_On_Grid
{
public partial class GridPage : System.Web.UI.Page
{
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["GridConnection"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack){
bindGrid();
}
}
public void bindGrid(){
sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand("select * from Grid",sqlCon);
SqlDataAdapter sqlda = new SqlDataAdapter(sqlCmd);
DataTable dt = new DataTable();
sqlda.Fill(dt);
sqlCon.Close();
TempGrid.DataSource= dt;
TempGrid.DataBind();
}

protected void TempGrid_RowEditing(object sender, GridViewEditEventArgs e){
TempGrid.EditIndex = e.NewEditIndex;
bindGrid();

}

protected void TempGrid_RowUpdating(object sender, GridViewUpdateEventArgs e){
Label Sid = TempGrid.Rows[e.RowIndex].FindControl("lblSid") as Label;
string name = ((TextBox)TempGrid.Rows[e.RowIndex].FindControl("txtSname")).Text;
string examtype = ((TextBox)TempGrid.Rows[e.RowIndex].FindControl("txtExamType")).Text;
string sub1 = ((TextBox)TempGrid.Rows[e.RowIndex].FindControl("txtSub1")).Text;
string sub2 = ((TextBox)TempGrid.Rows[e.RowIndex].FindControl("txtSub2")).Text;
string sub3 = ((TextBox)TempGrid.Rows[e.RowIndex].FindControl("txtSub3")).Text;
string total = ((TextBox)TempGrid.Rows[e.RowIndex].FindControl("txtTotal")).Text;
sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand("update Grid set Sname='"+name+"',ExamType='"+examtype+"',Subject1="+sub1+",Subject2="+sub2+",Subject3="+sub3+",Total="+total+" where Sid =" + Sid.Text,sqlCon);
sqlCmd.ExecuteNonQuery();
sqlCon.Close();
TempGrid.EditIndex = -1;
bindGrid();
}

protected void TempGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e){
TempGrid.EditIndex = -1;
bindGrid();
}

protected void TempGrid_RowDeleting(object sender, GridViewDeleteEventArgs e){
Label sid = TempGrid.Rows[e.RowIndex].FindControl("lblSid") as Label;
sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand("delete from Grid where Sid="+sid.Text,sqlCon);
int result = sqlCmd.ExecuteNonQuery();
sqlCon.Close();
if (result == 1)
bindGrid();
}
}
}


Thank You..

Comments or Responses

Login to post response