C#DataGridView怎么合并单元格。(WinForm)

Jave.Lin 2009-06-30 06:04:27
如题。

明天等大家的答案。
...全文
15069 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
youyoubaihehua 2010-08-30
  • 打赏
  • 举报
回复
哪可以下到C1FLEX控件
修改一下昵称 2009-07-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 hanhanmj 的回复:]
引用 1 楼 lfwer 的回复:
哪儿来的合并单元格一说·你以为是Table啊


哪儿来的不能合并单元格一说? 
[/Quote]
本来就是不能合并。
你那表面是实现了,那不也就是在dgv的CellPainting绘制事件里面画出来的。
suners 2009-07-02
  • 打赏
  • 举报
回复
好像网上有一个例子 在博客园上吧 你搜搜 看看 那解释挺详细的
peterb 2009-07-02
  • 打赏
  • 举报
回复
fengjian_428 2009-07-02
  • 打赏
  • 举报
回复
找个第三方控件来用嘛
kristy244 2009-07-02
  • 打赏
  • 举报
回复
LZ可以考虑一下用C1FLEX控件

调整格式挺方便的

包括单元格合并
单元格格式等等
hanhanmj 2009-07-02
  • 打赏
  • 举报
回复

public void dgCellPainting(DataGridView dgStandard, string strColumnName, DataGridViewCellPaintingEventArgs e)
{
if (dgStandard.Rows.Count > 0)
{

if (dgStandard.Rows[0].Cells[0].Value.ToString().Trim() != string.Empty)
{
try
{
if (dgStandard.Columns[strColumnName].Index == e.ColumnIndex && e.RowIndex >= 0)
{
using (
Brush gridBrush = new SolidBrush(dgStandard.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// 擦除原单元格背景
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

if (e.RowIndex != dgStandard.RowCount - 1)
{
if (e.Value.ToString() != dgStandard.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线
}
}
else
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线
}

//右侧的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, //x1,y1,x2,y2
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);

if (e.RowIndex == 0)
{
//绘制值
if (e.Value != null)
{

e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson,
e.CellBounds.X +(e.CellBounds.Width - e.Graphics.MeasureString(e.Value.ToString().Trim(), e.CellStyle.Font).Width)/2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
}
else
{
if (e.Value.ToString() != dgStandard.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString())
{
//绘制值
if (string.IsNullOrEmpty(e.Value.ToString().Trim()).Equals(false))
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X + (e.CellBounds.Width - e.Graphics.MeasureString(e.Value.ToString().Trim(), e.CellStyle.Font).Width) / 2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
}
}

e.Handled = true;
}
}
}
}
catch { }
}
}
}
hanhanmj 2009-07-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lfwer 的回复:]
哪儿来的合并单元格一说·你以为是Table啊
[/Quote]

哪儿来的不能合并单元格一说?
Jave.Lin 2009-07-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
C# codeprivatevoid dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)if (e.ColumnIndex==0&& e.RowIndex!=-1)
{using
(
Brush gridBrush=new SolidBrush(this.dataGridView1.GridColor),
backColorBrush=new SolidBrush(e.CellStyle.BackColor)
)
{using (Pen gridLinePen=new Pen(gridBrush))
{
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);if (e.RowIndex< dataGridView1.Rows.Count-1&&
dataGridView1.Rows[e.RowIndex+1].Cells[e.ColumnIndex].Value.ToString()!=
e.Value.ToString())
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom-1, e.CellBounds.Right-1,
e.CellBounds.Bottom-1);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right-1,
e.CellBounds.Top, e.CellBounds.Right-1,
e.CellBounds.Bottom);if (e.Value!=null)
{if (e.RowIndex>0&&
dataGridView1.Rows[e.RowIndex-1].Cells[e.ColumnIndex].Value.ToString()==
e.Value.ToString())
{ }else
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Black, e.CellBounds.X+2,
e.CellBounds.Y+5, StringFormat.GenericDefault);
}
}
e.Handled=true;
}
}
}
参考


[/Quote]

麻烦大哥加一下注释。

参考里的链接:HTTP连接超时。
surlew 2009-07-01
  • 打赏
  • 举报
回复
LZ可以看下我的博客里面有合并的例子!
wuyq11 2009-06-30
  • 打赏
  • 举报
回复
  private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

if (e.ColumnIndex == 0 && e.RowIndex != -1)
{
using
(
Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor)
)
{
using (Pen gridLinePen = new Pen(gridBrush))
{
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
if (e.RowIndex < dataGridView1.Rows.Count - 1 &&
dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() !=
e.Value.ToString())
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom);
if (e.Value != null)
{
if (e.RowIndex > 0 &&
dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() ==
e.Value.ToString())
{ }
else
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Black, e.CellBounds.X + 2,
e.CellBounds.Y + 5, StringFormat.GenericDefault);
}
}
e.Handled = true;
}
}
}

参考

lfwer 2009-06-30
  • 打赏
  • 举报
回复
哪儿来的合并单元格一说·你以为是Table啊

111,097

社区成员

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

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

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