首页 新闻 会员 周边 捐助

如何实现gridview多栏表头

0
[待解决问题]
<table border="0" cellspacing="0" cellpadding="0" width="434"> <colgroup span="1"><col span="1" width="120"></col><col span="1" width="106"></col><col span="1" width="105"></col><col span="1" width="103"></col></colgroup> <tbody> <tr height="18"> <td class="xl63" rowspan="2" width="120" height="36">客户</td> <td class="xl63" rowspan="2" width="106">欠款金额</td> <td class="xl63" colspan="2" width="208">&nbsp;&nbsp;&nbsp; 第一季度</td> </tr> <tr height="18"> <td class="xl63" height="18">金额</td> <td class="xl63">比例</td> </tr> </tbody> </table> <p>以上是gridview的表头,c#</p>
cuxin的主页 cuxin | 菜鸟二级 | 园豆:285
提问于:2011-02-13 13:05
< >
分享
所有回答(2)
0

<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>比率";
}
}

 

artwl | 园豆:16736 (专家六级) | 2011-02-14 12:09
0

借用上个兄弟的数据源.

前台代码如下:

<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);
}
}
}

 

Localhost | 园豆:443 (菜鸟二级) | 2011-02-14 15:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册