Thursday, October 20, 2011
Gridview with Color
Gridview Forecolor
Download code
Default.aspx
<%@ 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"
ShowFooter="true" AllowPaging="True"
DataKeyNames="id"
onpageindexchanging="GridView1_PageIndexChanging"
onrowdeleting="GridView1_RowDeleting"
onrowupdating="GridView1_RowUpdating" onrowediting="GridView1_RowEditing"
onrowcancelingedit="GridView1_RowCancelingEdit1" onrowdatabound="GridView1_RowDataBound"
>
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("id")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Eval("id")%>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("name")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%#Eval("name")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtName" runat="server" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblCity" runat="server" Text='<%#Eval("city")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCity" runat="server" Text='<%#Eval("city")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCity" runat="server" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<ItemTemplate>
<asp:Label ID="lblAge" runat="server" Text='<%#Eval("Age")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtAge" runat="server" Text='<%#Eval("Age")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAge" runat="server" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Command">
<ItemTemplate>
<asp:CheckBox ID="chkDel" runat="server" />
<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
<asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btnInsert" runat="server" Text="Insert" CommandName="Insert"
onclick="btnInsert_Click" />
<asp:Button ID="btnAll" runat="server" Text="Delete"
Font-Bold="False" onclick="btnAll_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
Name<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
City:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
Age:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
<asp:Button ID="btnInsert" runat="server" Text="Insert" CommandName="Add"
/>
</EmptyDataTemplate>
</asp:GridView>
</div>
</form>
</body>
</html>
Default.aspx.cs.
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
databind();
}
}
protected void databind()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from detail1 order by name ASC", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row;
row = GridView1.Rows[e.RowIndex];
string id = (row.FindControl("lblid") as Label).Text;
string name = (row.FindControl("txtName") as TextBox).Text;
string city = (row.FindControl("txtCity") as TextBox).Text;
string age = (row.FindControl("txtAge") as TextBox).Text;
con.Open();
SqlCommand cmd = new SqlCommand("Update detail1 set name='" + name + "',city='" + city + "',age='" + age + "' where id='" + id + "'", con);
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
con.Close();
databind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
con.Open();
System.Windows.Forms.DialogResult dr = System.Windows.Forms.MessageBox.Show("Are u sure", "Delete", System.Windows.Forms.MessageBoxButtons.OKCancel);
if (dr == System.Windows.Forms.DialogResult.OK)
{
int i = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
SqlCommand cmd = new SqlCommand("Delete from detail1 where id='" + i.ToString() + "'", con);
cmd.ExecuteNonQuery();
con.Close();
databind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
databind();
}
protected void btnInsert_Click(object sender, EventArgs e)
{
con.Open();
string name = (GridView1.FooterRow.FindControl("txtName") as TextBox).Text;
string city = (GridView1.FooterRow.FindControl("txtCity") as TextBox).Text;
string age = (GridView1.FooterRow.FindControl("txtAge") as TextBox).Text;
SqlCommand cmd = new SqlCommand("Insert into detail1 values('" + name + "','" + city + "','" + age + "')", con);
cmd.ExecuteNonQuery();
con.Close();
databind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
databind();
}
protected void GridView1_RowCancelingEdit1(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
databind();
}
protected void btnAll_Click(object sender, EventArgs e)
{
System.Windows.Forms.DialogResult dr = System.Windows.Forms.MessageBox.Show("Are u sure", "Delete", System.Windows.Forms.MessageBoxButtons.OKCancel);
if (dr == System.Windows.Forms.DialogResult.OK)
{
CheckBox chk;
foreach (GridViewRow row in GridView1.Rows)
{
chk = (CheckBox)row.FindControl("chkDel");
if (chk.Checked)
{
con.Open();
int i = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
SqlCommand cmd = new SqlCommand("Delete from detail1 where id='" + i.ToString() + "'", con);
cmd.ExecuteNonQuery();
con.Close();
databind();
}
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// Checking if row type
if (e.Row.RowType == DataControlRowType.DataRow)
{
// read the value from the datasoure
int id = Convert.ToInt32(Convert.ToString(DataBinder.Eval(e.Row.DataItem, "id")));
string name = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "name"));
string test = name.Substring(0, 1);
//Random r = new Random();
//int j = r.Next(1, 255);
//int k = r.Next(1, 255);
//int l = r.Next(1, 255);
//for (int i = 97; i <= 122; i++)
//{
// if (name.StartsWith(Convert.ToChar(i).ToString()))
// {
// // to change the row color by setting
// e.Row.BackColor = System.Drawing.Color.FromArgb(j,k,l);
// }
//}
switch (test.ToLower())
{
case "a":
e.Row.BackColor = System.Drawing.Color.AliceBlue;
break;
case "b":
e.Row.BackColor = System.Drawing.Color.AntiqueWhite;
break;
case "c":
e.Row.BackColor = System.Drawing.Color.Aqua;
break;
case "d":
e.Row.BackColor = System.Drawing.Color.Aquamarine;
break;
case "e":
e.Row.BackColor = System.Drawing.Color.Azure;
break;
case "f":
e.Row.BackColor = System.Drawing.Color.Beige;
break;
case "g":
e.Row.BackColor = System.Drawing.Color.Bisque;
break;
case "h":
e.Row.BackColor = System.Drawing.Color.BlanchedAlmond;
break;
case "i":
e.Row.BackColor = System.Drawing.Color.Blue;
break;
case "j":
e.Row.BackColor = System.Drawing.Color.BlueViolet;
break;
case "k":
e.Row.BackColor = System.Drawing.Color.Brown;
break;
case "l":
e.Row.BackColor = System.Drawing.Color.BurlyWood;
break;
case "m":
e.Row.BackColor = System.Drawing.Color.CadetBlue;
break;
case "n":
e.Row.BackColor = System.Drawing.Color.Chartreuse;
break;
case "o":
e.Row.BackColor = System.Drawing.Color.Chocolate;
break;
case "p":
e.Row.BackColor = System.Drawing.Color.Coral;
break;
case "q":
e.Row.BackColor = System.Drawing.Color.CornflowerBlue;
break;
case "r":
e.Row.BackColor = System.Drawing.Color.Cornsilk;
break;
case "s":
e.Row.BackColor = System.Drawing.Color.Cyan;
break;
case "t":
e.Row.BackColor = System.Drawing.Color.DarkBlue;
break;
case "u":
e.Row.BackColor = System.Drawing.Color.DarkGoldenrod;
break;
case "v":
e.Row.BackColor = System.Drawing.Color.DarkGreen;
break;
case "w":
e.Row.BackColor = System.Drawing.Color.DarkKhaki;
break;
case "x":
e.Row.BackColor = System.Drawing.Color.DarkMagenta;
break;
case "y":
e.Row.BackColor = System.Drawing.Color.DarkOrange;
break;
case "z":
e.Row.BackColor = System.Drawing.Color.DarkSalmon;
break;
}
}
}
}