Sunday, January 17, 2010

GridView Control



C# Code:

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;

public partial class _Default : System.Web.UI.Page
{
public static SqlConnection con;
public static SqlDataAdapter da;
public static SqlCommandBuilder cmdBuilder;
public static DataSet ds;
public static int SelectedRowIndex;
public static DataTable DTable;

public static string[] OldRowValue;
public static string[] NewRowValue;

protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack==false)
{
SelectedRowIndex = 0;
OldRowValue = new string[4];
NewRowValue = new string[4];
BindGrid();
ToggleSelectCol(true, false, false,false);
}
}

public void ToggleSelectCol(bool status1,bool status2,bool status3,bool status4)
{
GridView1.Columns[5].Visible =status1;
GridView1.Columns[6].Visible = status2;
GridView1.Columns[7].Visible = status3;
GridView1.Columns[8].Visible = status4;
}

public void BindGrid()
{
//pulling records from database
con = new SqlConnection("Initial Catalog=sangram;Integrated Security=SSPI;Data Source=(local)");
con.Open();
ds = new DataSet();
da = new SqlDataAdapter("select * from courses", con);
da.Fill(ds, "courses");

//setting commandbuilder
cmdBuilder = new SqlCommandBuilder(da);

//Adding Temp col in database
DataColumn dcol = new DataColumn();
dcol.DataType=System.Type.GetType("System.String");
dcol.AllowDBNull = false;
dcol.Caption = "Index";
dcol.ColumnName = "Index";

ds.Tables[0].Columns.Add(dcol);

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DataRow drow = ds.Tables[0].Rows[i];
drow["Index"] = i.ToString();
}
//Adding Primary key
AddPrimaryKey();

//binding data to gridview
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();

}

public void SlashColFromDataset()
{
DTable = new DataTable();
DTable = ds.Tables[0];
LblMessage.Text = "Original Table:" + ds.Tables[0].Columns.Count.ToString();
LblMessage.Text = LblMessage.Text + "New Table:" + DTable.Columns.Count.ToString();
DTable.Columns.Remove("Index");
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
SelectedRowIndex = Convert.ToInt16(e.CommandArgument);
GridView1.SelectedIndex = SelectedRowIndex;

//
DisableRow(false,true);
ToggleSelectCol(false,true,true,true);

//collecting old Values
GetOldValues();
//slash index col from table
SlashColFromDataset();

}
if (e.CommandName == "Insert")
{

}
if(e.CommandName=="CancelSelect")
{
GridView1.SelectedIndex = -1;
ToggleSelectCol(true, false, false, false);
DisableRow(true, true);
BindGrid();
}
if (e.CommandName == "Delete")
{
ToggleSelectCol(true,false,false,false);
DisableRow(true,true);
DeleteRecord();
BindGrid();
}

if (e.CommandName == "Edit")
{
GridView1.EditIndex = SelectedRowIndex;
BindGrid();

//other row remain in disable state
DisableRow(false, true);

//changing visiblity of col that doesn't apply
ToggleSelectCol(false, true, false,false);

//seting index of item to edit

}

if (e.CommandName == "Cancel")
{
ToggleSelectCol(true, false, false,false);
DisableRow(true, true);
GridView1.SelectedIndex = -1;
GridView1.EditIndex = -1;
BindGrid();

}

if (e.CommandName == "Update")
{
ToggleSelectCol(true, false, false,false);
DisableRow(true, true);


//collecting modified values
GetNewValue();

//Reflecting changes to database
if (NewRowValue[0] == OldRowValue[0])
{
UpdateRecord();
}
else
{
//need deletion of old row
DeleteRecord();
//insertion of new row
InsertRecord();

}


}
}
public void AddPrimaryKey()
{
DataColumn[] dcol = new DataColumn[1];
dcol[0] = ds.Tables[0].Columns[0];
ds.Tables[0].PrimaryKey = dcol;
}

public void DisableRow(bool other,bool selected)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridView1.Rows[i].Enabled = other ;
}
if (selected == true)
{
GridView1.Rows[SelectedRowIndex].Enabled = selected;
}
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridView1.SelectedIndex = -1;
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{

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

GridView1.EditIndex = -1;
GridView1.SelectedIndex = -1;
BindGrid();
}
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{

}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{


}

public void GetNewValue()
{
GridViewRow Row = GridView1.Rows[SelectedRowIndex];
int i = 0;
for (int j = 1; j < 5; j++)
{
foreach (object obj in Row.Cells[j].Controls)
{
if (obj is TextBox)
{
TextBox Txt = (TextBox)obj;
NewRowValue[i] = Txt.Text;
i++;
}
}
}
}

public void GetOldValues()
{
GridViewRow Row = GridView1.Rows[SelectedRowIndex];
int i = 0;
//As very first row is of Index not actually in database
for (int j = 1; j < 5; j++)
{
foreach (object obj in Row.Cells[j].Controls)
{
if (obj is Label)
{
Label Lbl = (Label)obj;
OldRowValue[i] = Lbl.Text;
i++;
}
}
}

}

public void UpdateRecord()
{
if (NewRowValue[0] == OldRowValue[0])
{
DataRow DRow = DTable.Rows.Find(OldRowValue[0]);
//need updating
for (int i = 0; i < 4; i++)
{
DRow[i] = NewRowValue[i];
}

//Updating DataAdapter
da.Update(DTable);
}
}

public void DeleteRecord()
{
//deleting selected row
DTable.Rows[SelectedRowIndex].Delete();

//Need to reflect changes in database
da.Update(DTable);
}

public void InsertRecord()
{
DataRow Drow = DTable.NewRow();

for (int i = 0; i < 4; i++)
{
Drow[i] = NewRowValue[i];
}
DTable.Rows.Add(Drow);
//Updating DataAdapter once for deletion and insertion
da.Update(DTable);
}
}

HTML CODE:

<%@ 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" OnRowCommand="GridView1_RowCommand" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Height="177px" Width="516px" >
<SelectedRowStyle BackColor="Aqua" />
<Columns>

<asp:TemplateField HeaderText="Index">
<ItemTemplate>
<asp:Label Text=<%#DataBinder.Eval(Container.DataItem,"index")%> ID="LblIndex" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>


<asp:TemplateField HeaderText="Course Id">
<ItemTemplate>
<asp:Label Text=<%#DataBinder.Eval(Container.DataItem,"cno")%> ID="LblCno" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Text=<%#DataBinder.Eval(Container.DataItem,"cno")%> ID="EditTxtCno" runat="server"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox Text="" ID="InsertTxtCno" runat="server"></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Course Name">
<ItemTemplate>
<asp:Label Text=<%#DataBinder.Eval(Container.DataItem,"cname")%> ID="LblCname" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Text=<%#DataBinder.Eval(Container.DataItem,"cname")%> ID="EditTxtCname" runat="server"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox Text="" ID="InsertTxtCname" runat="server"></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Course Duration">
<ItemTemplate>
<asp:Label Text=<%#DataBinder.Eval(Container.DataItem,"duration")%> ID="LblDuration" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Text=<%#DataBinder.Eval(Container.DataItem,"duration")%> ID="EditTxtDuration" runat="server"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox Text="" ID="InsertTxtDuration" runat="server"></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Course Fees">
<ItemTemplate>
<asp:Label Text=<%#DataBinder.Eval(Container.DataItem,"fees")%> ID="LblFees" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Text=<%#DataBinder.Eval(Container.DataItem,"fees")%> ID="EditTxtFees" runat="server"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox Text="" ID="InsertTxtFees" runat="server"></asp:TextBox>
</InsertItemTemplate>

</asp:TemplateField>

<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="BtnSelect" Text="Select" CommandName="Select" runat="server" CommandArgument=<%#DataBinder.Eval(Container.DataItem,"index")%> />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="BtnEdit" Text="Edit" CommandName="Edit" runat="server" CommandArgument=<%#DataBinder.Eval(Container.DataItem,"index")%> />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="BtnUpdate" Text="Update" CommandName="Update" runat="server" CommandArgument=<%#DataBinder.Eval(Container.DataItem,"index")%> />
<asp:Button ID="BtnCancel" Text="Cancel" CommandName="Cancel" runat="server" CommandArgument=<%#DataBinder.Eval(Container.DataItem,"index")%> />
</EditItemTemplate>
</asp:TemplateField>

<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Btndelete" Text="Delete" CommandName="Delete" runat="server" CommandArgument=<%#DataBinder.Eval(Container.DataItem,"index")%> />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="BtnCancelSelect" Text="Cancel Select" CommandName="CancelSelect" runat="server" CommandArgument=<%#DataBinder.Eval(Container.DataItem,"index")%> />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="BtnInsert" Text="Insert" CommandName="Insert" runat="server" CommandArgument=<%#DataBinder.Eval(Container.DataItem,"index")%> />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="LblMessage" runat="server"></asp:Label></div>
</form>
</body>
</html>

No comments:

Post a Comment