DATAGRIDVIEW的多维表头
我做的项目当要实现DATAGRIDVIEW的多维表头,时间很急不知道谁有人能够帮忙CSDN下载中有DLL文件可以做,但是不好用在项目里面,每次调用都要弹一个对话框要我和作者联系。.NET控件达人人有没有什么好的方法以共参考以下呢。或者好的第三方控件。。分不够可以在加!!!`
[解决办法]
一直用dev的控件,功能很強
[解决办法]
/// <summary> /// 支持双行表头的的DataGridView /// /// 用法示例: /// dg.AddSpanHeader(4, 4, "主标题 "); /// 则将第4列开始的4列设为双行表头,主标题为“主标题”,子标题为原来的 Value 值 /// /// phommy@hotmail.com /// </summary> public partial class DataGridViewEx1 : DataGridView { public DataGridViewEx1() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs pe) { // TODO: 在此处添加自定义绘制代码 // 调用基类 OnPaint base.OnPaint(pe); } private struct SpanInfo //表头信息 { public SpanInfo(string Text, int Position, int Left, int Right) { this.Text = Text; this.Position = Position; this.Left = Left; this.Right = Right; } public string Text; //列主标题 public int Position; //位置,1:左,2中,3右 public int Left; //对应左行 public int Right; //对应右行 } private Dictionary <int, SpanInfo> SpanRows = new Dictionary <int, SpanInfo> ();//需要2维表头的列 public void AddSpanHeader(int ColIndex, int ColCount, string Text) { if (ColCount < 2) throw new Exception( "行宽应大于等于2,合并1列无意义。 "); //检查范围 for (int i = 0; i < ColCount; i++) { if (SpanRows.ContainsKey(ColIndex + i)) throw new Exception( "单元格范围重叠! "); } //将这些列加入列表 int Right = ColIndex + ColCount - 1; //同一大标题下的最后一列的索引 SpanRows[ColIndex] = new SpanInfo(Text, 1, ColIndex, Right); //添加标题下的最左列 SpanRows[Right] = new SpanInfo(Text, 3, ColIndex, Right); //添加该标题下的最右列 for (int i = ColIndex + 1; i < Right; i++) //中间的列 { SpanRows[i] = new SpanInfo(Text, 2, ColIndex, Right); } } public void ClearSpanInfo() { SpanRows.Clear(); //ReDrawHead(); } private void DataGridViewEx_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex == -1) { if (SpanRows.ContainsKey(e.ColumnIndex)) //被合并的列 { //画边框 Graphics g = e.Graphics; e.Paint(e.CellBounds, DataGridViewPaintParts.Background | DataGridViewPaintParts.Border); int left = e.CellBounds.Left, top = e.CellBounds.Top + 2, right = e.CellBounds.Right, bottom = e.CellBounds.Bottom; switch (SpanRows[e.ColumnIndex].Position) { case 1: left += 2; break; case 2: break; case 3: right -= 2; break; } //画上半部分底色 g.FillRectangle(new SolidBrush(e.CellStyle.BackColor), left, top, right - left, (bottom - top) / 2); //画中线 g.DrawLine(new Pen(this.GridColor), left, (top + bottom) / 2, right, (top + bottom) / 2); //写小标题 StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; g.DrawString(e.Value + " ", e.CellStyle.Font, Brushes.Black, new Rectangle(left, (top + bottom) / 2, right - left, (bottom - top) / 2), sf); //写大标题 //if (this.SpanRows[e.ColumnIndex].Position==3) { left = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Left, true).Left - 2; if (left < 0) left = this.GetCellDisplayRectangle(-1, -1, true).Width; right = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Right, true).Right - 2; if (right < 0) right = this.Width; g.DrawString(SpanRows[e.ColumnIndex].Text, e.CellStyle.Font, Brushes.Black, new Rectangle(left, top, right - left, (bottom - top) / 2), sf); } e.Handled = true; } } } private void DataGridViewEx_Scroll(object sender, ScrollEventArgs e) { if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)// && e.Type == ScrollEventType.EndScroll) { timer1.Enabled = false; timer1.Enabled = true; } } //刷新显示表头 public void ReDrawHead() { foreach (int si in SpanRows.Keys) { this.Invalidate(this.GetCellDisplayRectangle(si, -1, true)); } } private void timer1_Tick(object sender, EventArgs e) { timer1.Enabled = false; ReDrawHead(); }
[解决办法]
示例代码: 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 = "第而列"; } } }
[解决办法]