<asp:GridView ID="GridView1" runat="server"
onrowcreated="GridView1_RowCreated1">
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
GridView1.BorderColor = System.Drawing.Color.DarkOrange;
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("客户", typeof(string)));
dt.Columns.Add(new DataColumn("欠款金额", typeof(decimal)));
dt.Columns.Add(new DataColumn("金额", typeof(decimal)));
dt.Columns.Add(new DataColumn("比率", typeof(string)));
for (int i = 0; i < 8; i++)
{
Random rd = new Random(Environment.TickCount * i);
dr = dt.NewRow();
dr[0] = "客户" + i.ToString();
dr[1] = Math.Round(rd.NextDouble() * 100, 2);
dr[2] = Math.Round(rd.NextDouble() * 100, 2);
dr[3] = Math.Round(rd.NextDouble() * 100, 2)+"%";
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
protected void GridView1_RowCreated1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header) //判断创建的行是不是标题行
{
TableCellCollection cells = e.Row.Cells;
cells.Clear(); //获得标题行,清空标题行的设置
cells.Add(new TableHeaderCell()); //添加一个标题单元
cells[0].RowSpan = 2; //设置跨行.
//直接导入html中的table中的元素
cells[0].Text = "客户</th><th rowspan=2>欠款金额</th><th colspan=2>第一季度</th></tr><tr bgcolor=white><th>金额</th><th>比率";
}
}
借用上个兄弟的数据源.
前台代码如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" onrowcreated="GridView1_RowCreated">
</asp:GridView>
</form>
</body>
</html>
public partial class UpImages_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("客户", typeof(string)));
dt.Columns.Add(new DataColumn("欠款金额", typeof(decimal)));
dt.Columns.Add(new DataColumn("金额", typeof(decimal)));
dt.Columns.Add(new DataColumn("比率", typeof(string)));
for (int i = 0; i < 8; i++)
{
Random rd = new Random(Environment.TickCount * i);
dr = dt.NewRow();
dr[0] = "客户" + i.ToString();
dr[1] = Math.Round(rd.NextDouble() * 100, 2);
dr[2] = Math.Round(rd.NextDouble() * 100, 2);
dr[3] = Math.Round(rd.NextDouble() * 100, 2) + "%";
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
/// <summary>
/// 生成表头
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells.Clear();
//添加"客户"列表头
TableHeaderCell cell1 = new TableHeaderCell();
cell1.Text = "客户";
cell1.RowSpan = 2;
e.Row.Cells.Add(cell1);
//添加"欠款金额"列表头
TableHeaderCell cell2 = new TableHeaderCell();
cell2.Text = "欠款金额";
cell2.RowSpan = 2;
e.Row.Cells.Add(cell2);
//添加"第一季度"列表头
//重点是Text
TableHeaderCell cell3 = new TableHeaderCell();
cell3.Text = "第一季度</th></tr><tr>";
cell3.ColumnSpan = 2;
e.Row.Cells.Add(cell3);
//添加"金额"列表头
TableHeaderCell cell4 = new TableHeaderCell();
cell4.Text = "金额";
e.Row.Cells.Add(cell4);
//添加"比例"列表头
TableHeaderCell cell5 = new TableHeaderCell();
cell5.Text = "比例";
e.Row.Cells.Add(cell5);
}
}
}