Sunday, January 17, 2010

Using Gridview Template:



HTML:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Cno">

<ItemTemplate>
<asp:Label ID="LblCno" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"cno") %> BorderStyle="Solid"></asp:Label>
</ItemTemplate>

<AlternatingItemTemplate>
<asp:Label ID="LblCno" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"cno") %> BorderStyle="Solid" BackColor="yellow"></asp:Label>
</AlternatingItemTemplate>

<EditItemTemplate>
<asp:TextBox runat="server" ID="TxtCno" Text=<%#DataBinder.Eval(Container.DataItem,"cno") %>></asp:TextBox>
</EditItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Cname">

<ItemTemplate>
<asp:Label ID="LblCname" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"cname") %> BorderStyle="Solid"></asp:Label>
</ItemTemplate>

<AlternatingItemTemplate>
<asp:Label ID="LblCname" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"cname") %> BorderStyle="Solid" BackColor="yellow"></asp:Label>
</AlternatingItemTemplate>

<EditItemTemplate>
<asp:TextBox runat="server" ID="TxtCname" Text=<%#DataBinder.Eval(Container.DataItem,"cname") %>></asp:TextBox>
</EditItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Duration">

<ItemTemplate>
<asp:Label ID="LblDuration" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"duration") %> BorderStyle="Solid"></asp:Label>
</ItemTemplate>

<AlternatingItemTemplate>
<asp:Label ID="LblDuration" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"duration") %> BorderStyle="Solid" BackColor="yellow"></asp:Label>
</AlternatingItemTemplate>

<EditItemTemplate>
<asp:TextBox runat="server" ID="TxtDuration" Text=<%#DataBinder.Eval(Container.DataItem,"duration") %>></asp:TextBox>
</EditItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Fees">

<ItemTemplate>
<asp:Label ID="LblFees" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"fees") %> BorderStyle="Solid"></asp:Label>
</ItemTemplate>

<AlternatingItemTemplate>
<asp:Label ID="LblFees" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"fees") %> BorderStyle="Solid" BackColor="yellow"></asp:Label>
</AlternatingItemTemplate>

<EditItemTemplate>
<asp:TextBox runat="server" ID="TxtFees" Text=<%#DataBinder.Eval(Container.DataItem,"fees") %>></asp:TextBox>
</EditItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Command">

<ItemTemplate>
<asp:Button ID="Btn1" runat="server" Text="Edit" CommandArgument=<%#cursor++ %> CommandName="Edit"></asp:button>
</ItemTemplate>

<AlternatingItemTemplate>
<asp:Button ID="Btn1" runat="server" Text="Edit" BackColor="yellow" CommandArgument=<%#cursor++ %> CommandName="Edit"></asp:button>
</AlternatingItemTemplate>

<EditItemTemplate>
<asp:Button ID="Btn1" runat="server" Text="Update" CommandArgument=<%#cursor++ %> CommandName="Update"></asp:button>
<asp:Button ID="Btn2" runat="server" Text="Cancel" CommandArgument=<%#cursor++ %> CommandName="Cancel"></asp:button>
</EditItemTemplate>

</asp:TemplateField>
</Columns>

</asp:GridView>
<asp:Label ID="LblMessage" runat="server"></asp:Label></div>
</form>
</body>
</html>


C#:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
public static int cursor;
public static string[] OldRowValues;
public static string[] NewRowValues;
public static SqlDataAdapter da;
public static DataSet ds;
public static SqlCommandBuilder cmdBuilder;
public static SqlConnection con;

protected void Page_Load(object sender, EventArgs e)
{
if (! Page.IsPostBack)
{
OldRowValues = new string[4];
NewRowValues = new string[4];
cursor = 0;
Bind();
}

}
public void Bind()
{
con = new SqlConnection("Initial Catalog=sangram;Integrated Security=SSPI;Data Source=(local);");
con.Open();
da = new SqlDataAdapter("select * from courses", con);
cmdBuilder = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds, "Courses");

DataColumn[] dcol = new DataColumn[1];
dcol[0] = ds.Tables[0].Columns[0];
ds.Tables[0].PrimaryKey = dcol;

GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewEditIndex];

for (int i = 0; i < row.Cells.Count; i++)
{
foreach (object obj in row.Cells[i].Controls)
{
if (obj is Label)
{

OldRowValues[i] = ((Label)obj).Text;
}
}
}
//code to update data


GridView1.EditIndex = e.NewEditIndex;
Bind();


}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.SelectedIndex = -1;
e.Cancel = true;
Bind();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = e.RowIndex;
GridViewRow row = GridView1.Rows[e.RowIndex];

for (int i = 0; i < row.Cells.Count; i++)
{
foreach (object obj in row.Cells[i].Controls)
{
if (obj is TextBox)
{
NewRowValues[i] = ((TextBox)obj).Text;
}

}
}

if (NewRowValues[0] == OldRowValues[0])
{
//need only to update
DataRow drow = ds.Tables[0].Rows.Find(OldRowValues[0]);

drow[0] = NewRowValues[0];
drow[1] = NewRowValues[1];
drow[2] = NewRowValues[2];
drow[3] = NewRowValues[3];

da.Update(ds, "Courses");

}
else
{
//need deletion of initial row
ds.Tables[0].Rows.Find(OldRowValues[0]).Delete();

//insertion of new row
DataRow drow = ds.Tables[0].NewRow();
drow[0] = NewRowValues[0];
drow[1] = NewRowValues[1];
drow[2] = NewRowValues[2];
drow[3] = NewRowValues[3];

ds.Tables[0].Rows.Add(drow);

da.Update(ds, "Courses");
}

GridView1.EditIndex = -1;
GridView1.SelectedIndex = -1;
Bind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

}
}

No comments:

Post a Comment