DATAGRIDVIEW的多维表头

hnjhjzyyxgs 2009-03-18 12:42:25
我做的项目当要实现DATAGRIDVIEW的多维表头,时间很急不知道谁有人能够帮忙CSDN下载中有DLL文件可以做,但是不好用在项目里面,每次调用都要弹一个对话框要我和作者联系。.NET控件达人人有没有什么好的方法以共参考以下呢。或者好的第三方控件。。分不够可以在加!!!`
...全文
1286 48 打赏 收藏 转发到动态 举报
写回复
用AI写文章
48 条回复
切换为时间正序
请发表友善的回复…
发表回复
zb19820625 2012-10-15
  • 打赏
  • 举报
回复
还需要吗,我这有源码VB.NET2010的
appleller 2010-04-29
  • 打赏
  • 举报
回复
同感。。。
dency1015 2010-03-31
  • 打赏
  • 举报
回复
都是让人去下载,分都下完了,也没有下到想要的,大家回帖负责点好不好啊
dency1015 2010-03-31
  • 打赏
  • 举报
回复
这么多层楼,没有一个特别好的答案。
Tensionli 2009-05-13
  • 打赏
  • 举报
回复
如果可以使用第三方控件的话,用devexpress的gridcontrol,它可以实现你要的多层表对,找一个可以看看,dev里都有DEMO,我们做多表头都是用它做的,就是挺吃资源的。
光宇广贞 2009-05-13
  • 打赏
  • 举报
回复
[Quote=引用 38 楼 hnjhjzyyxgs 的回复:]
引用 37 楼 gengxin_914 的回复:
第三方的控件:farpoint!

此控件能做三层表头吗? 如果可以分全部给你了
[/Quote]

http://www.farpoint.com.cn/
楼主先看看这个再说,可以下载评估版,自己用一下,觉得好用的而且又需要完整版的话,可以去买,或者可以通过对此使用的体验说出更详细的控件使用需求,这样大家回答或者帮助起来更有方向。

程序员不喜欢去猜用户可能需要什么样的软件……
glboy12 2009-05-13
  • 打赏
  • 举报
回复
<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
// 计算数据,完全可以从数据看取得
ICollection CreateDataSource( )
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal)));

for (int i = 0; i < 8; i++)
{
System.Random rd = new System.Random(Environment.TickCount * i); ;
dr = dt.NewRow();
dr[0] = "班级" + i.ToString();
dr[1] = "学生" + 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,相当于表格的 TR 一行
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
string HeaderBackColor = "#EDEDED";
rowHeader.BackColor = System.Drawing.ColorTranslator.FromHtml(HeaderBackColor);

//实现确定要显示的表头样式,也可以通过计算生成

// <tr>
// <td rowspan='2'>关键单元格</td>
// <td colspan='2'>表头文字</td>
// <td colspan='2'>表头文字</td>
// <td>表头文字</td>
// </tr>
// <tr bgcolor='#FFF'>
// <td colspan='2'>表头文字</td>
// <td rowspan='2'>表头文字</td>
// <td colspan='2'>表头文字</td>
// </tr>
// <tr bgcolor='#FFF'>
// <td>表头文字</td>
// <td>表头文字</td>
// <td>表头文字</td>
// <td>表头文字</td>
// <td>表头文字</td>";
// </tr>
// 上面的样式可以设置斜线

Literal newCells = new Literal();
newCells.Text = @"表头文字1</th>
<th colspan='2'>表头文字2</th>
<th colspan='2'>表头文字3</th>
<th>表头文字4</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th colspan='2'>表头文字5</th>
<th rowspan='2'>表头文字6</th>
<th colspan='2'>表头文字7</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th>表头文字8</th>
<th>表头文字9</th>
<th>表头文字10</th>
<th>表头文字11</th>
<th>表头文字12";

TableCellCollection cells = e.Row.Cells;
TableHeaderCell headerCell = new TableHeaderCell();
//下面的属性设置与 <td rowspan='2'>关键单元格</td> 要一致
headerCell.RowSpan = 2;
headerCell.Controls.Add(newCells);
rowHeader.Cells.Add(headerCell);

rowHeader.Cells.Add(headerCell);
rowHeader.Visible = true;

//添加到 GridView1
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");
}
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>为 GridView 添加多层表头</title>
</head>
<body>
<form id="Form1" runat="server">
<asp:GridView ID="GridView1" runat="server" CellSpacing="1" CellPadding="3" Font-Size="12px"
Width="600px" BackColor="#000000" BorderWidth="0" OnRowDataBound="GridView1_RowDataBound"
OnRowCreated="GridView1_RowCreated">
</asp:GridView>
</form>
</body>
</html>
修改一下昵称 2009-05-10
  • 打赏
  • 举报
回复
帮顶。关注,学习!
hnjhjzyyxgs 2009-05-10
  • 打赏
  • 举报
回复
没人了吗?
hnjhjzyyxgs 2009-05-07
  • 打赏
  • 举报
回复
[Quote=引用 37 楼 gengxin_914 的回复:]
第三方的控件:farpoint!
[/Quote]
此控件能做三层表头吗? 如果可以分全部给你了
gengxin_914 2009-05-06
  • 打赏
  • 举报
回复
第三方的控件:farpoint!
weir55 2009-04-19
  • 打赏
  • 举报
回复
<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
// 计算数据,完全可以从数据看取得
ICollection CreateDataSource( )
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal)));

for (int i = 0; i < 8; i++)
{
System.Random rd = new System.Random(Environment.TickCount * i); ;
dr = dt.NewRow();
dr[0] = "班级" + i.ToString();
dr[1] = "学生" + 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,相当于表格的 TR 一行
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
string HeaderBackColor = "#EDEDED";
rowHeader.BackColor = System.Drawing.ColorTranslator.FromHtml(HeaderBackColor);

//实现确定要显示的表头样式,也可以通过计算生成

// <tr>
// <td rowspan='2'>关键单元格</td>
// <td colspan='2'>表头文字</td>
// <td colspan='2'>表头文字</td>
// <td>表头文字</td>
// </tr>
// <tr bgcolor='#FFF'>
// <td colspan='2'>表头文字</td>
// <td rowspan='2'>表头文字</td>
// <td colspan='2'>表头文字</td>
// </tr>
// <tr bgcolor='#FFF'>
// <td>表头文字</td>
// <td>表头文字</td>
// <td>表头文字</td>
// <td>表头文字</td>
// <td>表头文字</td>";
// </tr>
// 上面的样式可以设置斜线

Literal newCells = new Literal();
newCells.Text = @"表头文字1</th>
<th colspan='2'>表头文字2</th>
<th colspan='2'>表头文字3</th>
<th>表头文字4</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th colspan='2'>表头文字5</th>
<th rowspan='2'>表头文字6</th>
<th colspan='2'>表头文字7</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th>表头文字8</th>
<th>表头文字9</th>
<th>表头文字10</th>
<th>表头文字11</th>
<th>表头文字12";

TableCellCollection cells = e.Row.Cells;
TableHeaderCell headerCell = new TableHeaderCell();
//下面的属性设置与 <td rowspan='2'>关键单元格</td> 要一致
headerCell.RowSpan = 2;
headerCell.Controls.Add(newCells);
rowHeader.Cells.Add(headerCell);

rowHeader.Cells.Add(headerCell);
rowHeader.Visible = true;

//添加到 GridView1
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");
}
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>为 GridView 添加多层表头</title>
</head>
<body>
<form id="Form1" runat="server">
<asp:GridView ID="GridView1" runat="server" CellSpacing="1" CellPadding="3" Font-Size="12px"
Width="600px" BackColor="#000000" BorderWidth="0" OnRowDataBound="GridView1_RowDataBound"
OnRowCreated="GridView1_RowCreated">
</asp:GridView>
</form>
</body>
</html>
热学沸腾56 2009-04-19
  • 打赏
  • 举报
回复
帮顶!
hnjhjzyyxgs 2009-04-19
  • 打赏
  • 举报
回复
[Quote=引用 32 楼 tyq_man 的回复:]
C# 实现GRIDVIEW 复杂表头 http://www.builder.com.cn/2007/1216/683752.shtml
[/Quote]
我说的是winform的
tyq_man 2009-04-16
  • 打赏
  • 举报
回复
C# 实现GRIDVIEW 复杂表头 http://www.builder.com.cn/2007/1216/683752.shtml
uncleson88 2009-04-16
  • 打赏
  • 举报
回复
试试reportviewer?很轻松就可以画出多维表头
itcrazyman 2009-04-15
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 zzxap 的回复:]
有可以用嵌套datagridview实现,只要你动一下脑筋就行了,

嵌套datagridview
http://download.csdn.net/source/944709
[/Quote]
mark up 帮顶
zzxap 2009-04-15
  • 打赏
  • 举报
回复
有可以用嵌套datagridview实现,只要你动一下脑筋就行了,

嵌套datagridview
http://download.csdn.net/source/944709
zzxap 2009-04-15
  • 打赏
  • 举报
回复
[code=C#]
示例代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataTable table = new DataTable();
DataColumn c1 = new DataColumn("ID", typeof(int));
table.Columns.Add(c1);
DataColumn c2 = new DataColumn("name", typeof(string));
table.Columns.Add(c2);

DataRow r1 = table.NewRow();
r1["ID"] = 1;
r1["name"] = "abc";
table.Rows.Add(r1);
DataRow r2 = table.NewRow();
r2["ID"] = 2;
r2["name"] = "bcd";
table.Rows.Add(r2);
this.dataGridView1.DataSource = table;
}
//改变HeaderText
private void button2_Click(object sender, EventArgs e)
{
this.dataGridView1.Columns["ID"].HeaderText = "第一列";
this.dataGridView1.Columns["name"].HeaderText = "第而列";
}

}
}
[/CODE]
zzxap 2009-04-15
  • 打赏
  • 举报
回复
[CODE=C#]
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name= "disposing "> 如果应释放托管资源,为 true;否则为 false。 </param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region 组件设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
this.SuspendLayout();
//
// timer1
//
this.timer1.Interval = 20;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// DataGridViewEx1
//
this.RowTemplate.Height = 23;
this.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.DataGridViewEx_CellPainting);
this.Scroll += new System.Windows.Forms.ScrollEventHandler(this.DataGridViewEx_Scroll);
((System.ComponentModel.ISupportInitialize)(this)).EndInit();
this.ResumeLayout(false);

}
#endregion

private System.Windows.Forms.Timer timer1;
}
[/CODE]
加载更多回复(27)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧