如何合并DataGrid中的行? 在线等待 本帖最后由 xiongzhiqiang123 于 2009-09-24 13:47:53 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 合并行的DataGridView方法[转]2009年01月14日 星期三 17:30using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace MultiHeaderDataGridView{ public partial class RowUnitView : DataGridView { public RowUnitView() { InitializeComponent(); this.MultiSelect = true; this.SelectionMode = DataGridViewSelectionMode.FullRowSelect; } private int i = 1; private int count2 = 1; private int flag; string selectedValue; protected override void OnPaint(PaintEventArgs pe) { // TODO: 在此处添加自定义绘制代码 // 调用基类 OnPaint base.OnPaint(pe); } protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e) { try { DrawCell(e); base.OnCellPainting(e); } catch { } } /// <summary> /// 画单元格 /// </summary> /// <param name="e"></param> private void DrawCell(DataGridViewCellPaintingEventArgs e) { Brush gridBrush = new SolidBrush(this.GridColor); SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor); SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor); int cellheight; int fontheight; int cellwidth; int fontwidth; int countU = 0; int countD = 0; int count = 0; // 对第1列相同单元格进行合并 if (e.ColumnIndex == 0 && e.RowIndex != -1) { cellheight = e.CellBounds.Height; fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height; cellwidth = e.CellBounds.Width; fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width; Pen gridLinePen = new Pen(gridBrush); string curValue = e.Value == null ? "" : e.Value.ToString().Trim(); string curSelected = this.CurrentRow.Cells[0].Value == null ? "" : this.CurrentRow.Cells[0].Value.ToString().Trim(); if (!string.IsNullOrEmpty(curValue)) { for (int i = e.RowIndex; i < this.Rows.Count; i++) { if (this.Rows[i].Cells[0].Value.ToString().Equals(curValue)) { this.Rows[i].Cells[0].Selected = this.Rows[e.RowIndex].Selected; this.Rows[i].Selected = this.Rows[e.RowIndex].Selected; countD++; } else { break; } } for (int i = e.RowIndex; i >= 0; i--) { if (this.Rows[i].Cells[0].Value.ToString().Equals(curValue)) { this.Rows[i].Cells[0].Selected = this.Rows[e.RowIndex].Selected; this.Rows[i].Selected = this.Rows[e.RowIndex].Selected; countU++; } else { break; } } count = countD + countU -1; if (count < 2) { return; } } if (this.Rows[e.RowIndex].Selected) { backBrush.Color = e.CellStyle.SelectionBackColor; fontBrush.Color = e.CellStyle.SelectionForeColor; } e.Graphics.FillRectangle(backBrush, e.CellBounds); //new Rectangle(e.CellBounds.X,e.CellBounds.Y-(countU-1) * cellheight,cellwidth,cellheight*countU)); e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (countU - 1) + (cellheight * count - fontheight) / 2); if (countD == 1) { e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); count = 0; } // 画右边线 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right-1 , e.CellBounds.Top, e.CellBounds.Right-1 , e.CellBounds.Bottom); e.Handled = true; } //else //{ // // 对第2列相同单元格进行合并 // if (e.ColumnIndex == 1 && e.RowIndex != -1) // { // h = e.CellBounds.Height; // wh = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height; // using (Pen gridLinePen = new Pen(gridBrush)) // { // // 清除单元格 // e.Graphics.FillRectangle(backBrush, e.CellBounds); // // 画 Grid 边线(仅画单元格的底边线和右边线) // //如果本行的港口画了一条底边线,则在当前的单元格画一条底边线,并填充数据 // if (flag == 1) // { // e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); // e.Graphics.DrawString(e.Value == null ? "" : e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + h - (h * count2 - wh) / 2 - wh); // count2 = 1; // flag = 0; // } // else // { // count2 = count2 + 1; // } // // 画右边线 // e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); // } // e.Handled = true; // } //} } protected override void OnCellClick(DataGridViewCellEventArgs e) { base.OnCellClick(e); if(e.RowIndex>-1) this.Rows[e.RowIndex].Cells[0].Selected = true; } }} 很多吧 DevExpress的XtraGrid就可以,不过估计你不会用 /// <summary> /// ///合并单元格 /// </summary> /// <param name="gv"></param> protected void Unite(GridView gv) { int i; string LastType1; int LastCell; if (gv.Rows.Count > 0) { LastType1 = gv.Rows[0].Cells[0].Text; gv.Rows[0].Cells[0].RowSpan = 1; LastCell = 0; for (i = 1; i < gv.Rows.Count; i++) { if (gv.Rows[i].Cells[0].Text == LastType1) { gv.Rows[i].Cells[0].Visible = false; gv.Rows[LastCell].Cells[0].RowSpan++; } else { LastType1 = gv.Rows[i].Cells[0].Text; LastCell = i; gv.Rows[i].Cells[0].RowSpan = 1; } } } } if (!Page.IsPostBack) { Unite(GridView1);//合并单元格 } 请教两个问题,都是和WPF的数据绑定相关的,求解答 谁能帮我看下这个错误,谢谢 网格论 自定义文件的问题 用Tab做分隔符的文件能直接作为数据源读取吗? 关于文章管理系统FreeTextBox控件 在线等 做好个论坛。测试数据30万 如何读取接收到邮件中的内容 请问如何实现自现在起150秒后,每5分钟执行一个方法 水晶报表不预览每一页打印格式就不对,打印的时候把每一页都预览一次就是对的,请遇到过这个问题的朋友帮忙解决一下 C#读取GPS数据的问题 c#编码问题
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace MultiHeaderDataGridView
{
public partial class RowUnitView : DataGridView
{
public RowUnitView()
{
InitializeComponent();
this.MultiSelect = true;
this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
private int i = 1;
private int count2 = 1;
private int flag;
string selectedValue;
protected override void OnPaint(PaintEventArgs pe)
{
// TODO: 在此处添加自定义绘制代码 // 调用基类 OnPaint
base.OnPaint(pe);
}
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
try
{
DrawCell(e);
base.OnCellPainting(e);
}
catch
{ }
}
/// <summary>
/// 画单元格
/// </summary>
/// <param name="e"></param>
private void DrawCell(DataGridViewCellPaintingEventArgs e)
{
Brush gridBrush = new SolidBrush(this.GridColor);
SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);
SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
int cellheight;
int fontheight;
int cellwidth;
int fontwidth;
int countU = 0;
int countD = 0;
int count = 0;
// 对第1列相同单元格进行合并
if (e.ColumnIndex == 0 && e.RowIndex != -1)
{
cellheight = e.CellBounds.Height;
fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
cellwidth = e.CellBounds.Width;
fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
Pen gridLinePen = new Pen(gridBrush);
string curValue = e.Value == null ? "" : e.Value.ToString().Trim();
string curSelected = this.CurrentRow.Cells[0].Value == null ? "" : this.CurrentRow.Cells[0].Value.ToString().Trim();
if (!string.IsNullOrEmpty(curValue))
{
for (int i = e.RowIndex; i < this.Rows.Count; i++)
{
if (this.Rows[i].Cells[0].Value.ToString().Equals(curValue))
{
this.Rows[i].Cells[0].Selected = this.Rows[e.RowIndex].Selected;
this.Rows[i].Selected = this.Rows[e.RowIndex].Selected;
countD++;
}
else
{
break;
}
}
for (int i = e.RowIndex; i >= 0; i--)
{
if (this.Rows[i].Cells[0].Value.ToString().Equals(curValue))
{
this.Rows[i].Cells[0].Selected = this.Rows[e.RowIndex].Selected;
this.Rows[i].Selected = this.Rows[e.RowIndex].Selected;
countU++;
}
else
{
break;
}
}
count = countD + countU -1;
if (count < 2) { return; }
}
if (this.Rows[e.RowIndex].Selected)
{
backBrush.Color = e.CellStyle.SelectionBackColor;
fontBrush.Color = e.CellStyle.SelectionForeColor;
} e.Graphics.FillRectangle(backBrush, e.CellBounds); //new Rectangle(e.CellBounds.X,e.CellBounds.Y-(countU-1) * cellheight,cellwidth,cellheight*countU)); e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (countU - 1) + (cellheight * count - fontheight) / 2); if (countD == 1)
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
count = 0;
}
// 画右边线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right-1 , e.CellBounds.Top, e.CellBounds.Right-1 , e.CellBounds.Bottom);
e.Handled = true;
}
//else
//{
// // 对第2列相同单元格进行合并
// if (e.ColumnIndex == 1 && e.RowIndex != -1)
// {
// h = e.CellBounds.Height;
// wh = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height; // using (Pen gridLinePen = new Pen(gridBrush))
// {
// // 清除单元格
// e.Graphics.FillRectangle(backBrush, e.CellBounds);
// // 画 Grid 边线(仅画单元格的底边线和右边线)
// //如果本行的港口画了一条底边线,则在当前的单元格画一条底边线,并填充数据
// if (flag == 1)
// {
// e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); // e.Graphics.DrawString(e.Value == null ? "" : e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + h - (h * count2 - wh) / 2 - wh); // count2 = 1; // flag = 0; // }
// else
// {
// count2 = count2 + 1;
// }
// // 画右边线
// e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); // }
// e.Handled = true;
// }
//}
}
protected override void OnCellClick(DataGridViewCellEventArgs e)
{
base.OnCellClick(e);
if(e.RowIndex>-1)
this.Rows[e.RowIndex].Cells[0].Selected = true;
} }
}
/// ///合并单元格
/// </summary>
/// <param name="gv"></param>
protected void Unite(GridView gv)
{
int i;
string LastType1;
int LastCell; if (gv.Rows.Count > 0)
{
LastType1 = gv.Rows[0].Cells[0].Text;
gv.Rows[0].Cells[0].RowSpan = 1;
LastCell = 0;
for (i = 1; i < gv.Rows.Count; i++)
{
if (gv.Rows[i].Cells[0].Text == LastType1)
{
gv.Rows[i].Cells[0].Visible = false;
gv.Rows[LastCell].Cells[0].RowSpan++;
}
else
{
LastType1 = gv.Rows[i].Cells[0].Text;
LastCell = i;
gv.Rows[i].Cells[0].RowSpan = 1;
}
}
}
}
{
Unite(GridView1);//合并单元格
}