|
 |
The ICollection interface is the base interface for classes in the System.Collections namespace. Dictionary and IList are more specialized interfaces that are based on the ICollection interface. An IDictionary implementation is a collection of key-and-value pairs, like the Hashtable class. An IList implementation is a collection of values that can be sorted and whose members can be accessed by index, like the Array List class.We use the DataTable to add column. And use the DataRow to add row. We use the Literal control to reserve a location on the Web page to display text. The Literal control is similar to the Label control, except the Literal control does not allow you to apply a style to the displayed text. You can programmatically control the text displayed in the control by setting the Text property.
We use the Literal control to add complex head.
This tutorial will show you how to add GridViewComplexHead using ASP.NET 2.0 and C#.NET .First, you need to import the System.Collections namespace.
We can add complex table head to GridView. We use the Literal control to add table head.
|
using System.Collections; |
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
The ICollection interface is the base interface for classes in the System.Collections namespace. Dictionary and IList are more specialized interfaces that are based on the ICollection interface. An IDictionary implementation is a collection of key-and-value pairs, like the Hashtable class. An IList implementation is a collection of values that can be sorted and whose members can be accessed by index, like the Array List class.We use the DataTable to add column. And use the DataRow to add row. We use the Literal control to reserve a location on the Web page to display text. The Literal control is similar to the Label control, except the Literal control does not allow you to apply a style to the displayed text. You can programmatically control the text displayed in the control by setting the Text property.
We use the Literal control to add complex head.
We use the Page_Load event to display data. And use the GridView1_RowCreated event to create complex head. We use the GridView1_RowDataBound to set color.
public partial class GridViewComplexHead : System.Web.UI.Page
{
ICollection CreateDataSource()
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("Class", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("Name", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("Literature", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("Math", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("English", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("Computer", typeof(System.Decimal)));
for (int i = 0; i < 8; i++)
{
System.Random rd = new System.Random(Environment.TickCount * i);
dr = dt.NewRow();
dr[0] = "Class" + i.ToString();
dr[1] = "Student" + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
dt.Rows.Add(dr);
}
System.Data.DataView dv = new System.Data.DataView(dt);
return dv;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.BorderColor = System.Drawing.Color.DarkOrange;
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
string HeaderBackColor = "#EDEDED";
rowHeader.BackColor = System.Drawing.ColorTranslator.FromHtml(HeaderBackColor);
Literal newCells = new Literal();
newCells.Text = @"Table Head letter1</th>
<th colspan='2'>Table Head letter2</th>
<th colspan='2'>Table Head letter3</th>
<th>Table Head letter4</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th colspan='2'>Table Head letter5</th>
<th rowspan='2'>Table Head letter6</th>
<th colspan='2'>Table Head letter7</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th>Table Head letter8</th>
<th>Table Head letter9</th>
<th>Table Head letter10</th>
<th>Table Head letter11</th>
<th>Table Head letter12";
TableCellCollection cells = e.Row.Cells;
TableHeaderCell headerCell = new TableHeaderCell();
headerCell.RowSpan = 2;
headerCell.Controls.Add(newCells);
rowHeader.Cells.Add(headerCell);
rowHeader.Cells.Add(headerCell);
rowHeader.Visible = true;
GridView1.Controls[0].Controls.AddAt(0, rowHeader);
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Attributes.Add("style", "background:#9999FF;color:#FFFFFF;font-size:14px");
}
else
{
e.Row.Attributes.Add("style", "background:#FFF");
}
}
}
|
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
The front end Default.aspx page looks something like this:
<asp:GridView ID="GridView1" runat="server" CellSpacing="1" CellPadding="3" Font-Size="12px"
Width="600px" BackColor="Transparent" BorderWidth="0px" OnRowDataBound="GridView1_RowDataBound"
OnRowCreated="GridView1_RowCreated">
</asp:GridView>
|
We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
The flow for the code behind page is as follows.
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.Collections;
public partial class GridViewComplexHead : System.Web.UI.Page
{
ICollection CreateDataSource()
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("Class", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("Name", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("Literature", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("Math", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("English", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("Computer", typeof(System.Decimal)));
for (int i = 0; i < 8; i++)
{
System.Random rd = new System.Random(Environment.TickCount * i);
dr = dt.NewRow();
dr[0] = "Class" + i.ToString();
dr[1] = "Student" + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
dt.Rows.Add(dr);
}
System.Data.DataView dv = new System.Data.DataView(dt);
return dv;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.BorderColor = System.Drawing.Color.DarkOrange;
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
string HeaderBackColor = "#EDEDED";
rowHeader.BackColor = System.Drawing.ColorTranslator.FromHtml(HeaderBackColor);
Literal newCells = new Literal();
newCells.Text = @"Table Head letter1</th>
<th colspan='2'>Table Head letter2</th>
<th colspan='2'>Table Head letter3</th>
<th>Table Head letter4</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th colspan='2'>Table Head letter5</th>
<th rowspan='2'>Table Head letter6</th>
<th colspan='2'>Table Head letter7</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th>Table Head letter8</th>
<th>Table Head letter9</th>
<th>Table Head letter10</th>
<th>Table Head letter11</th>
<th>Table Head letter12";
TableCellCollection cells = e.Row.Cells;
TableHeaderCell headerCell = new TableHeaderCell();
headerCell.RowSpan = 2;
headerCell.Controls.Add(newCells);
rowHeader.Cells.Add(headerCell);
rowHeader.Cells.Add(headerCell);
rowHeader.Visible = true;
GridView1.Controls[0].Controls.AddAt(0, rowHeader);
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Attributes.Add("style", "background:#9999FF;color:#FFFFFF;font-size:14px");
}
else
{
e.Row.Attributes.Add("style", "background:#FFF");
}
}
}
|
Download the Full Working Version of this Project written with Visual Studio.NET C# .NET 2005 Here!
Looking for the VB.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!
|
|
|