datagrivdview中同一列或同一行中相临的单元格中相同是怎样合并成一个单元格

renchuang 2008-11-15 12:42:32
datagrivdview中同一列或同一行中相临的单元格中相同是怎样合并成一个单元格
...全文
108 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gouhuafeng 2008-11-15
  • 打赏
  • 举报
回复
Private nextrow As System.Nullable(Of Integer) = Nothing
Private nextcol As System.Nullable(Of Integer) = Nothing
Private Sub dataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs)
If Me.dataGridView1.Columns("description").Index = e.ColumnIndex AndAlso e.RowIndex >= 0 Then
If Me.nextcol IsNot Nothing And e.ColumnIndex = Me.nextcol Then
e.CellStyle.BackColor = Color.LightBlue
Me.nextcol = Nothing
End If
If Me.nextrow IsNot Nothing And e.RowIndex = nextrow Then
e.CellStyle.BackColor = Color.LightPink
Me.nextrow = Nothing
End If
If e.RowIndex <> Me.dataGridView1.RowCount - 1 Then
If e.Value.ToString() = Me.dataGridView1.Rows(e.RowIndex + 1).Cells(e.ColumnIndex).Value.ToString() Then
e.CellStyle.BackColor = Color.LightPink
nextrow = e.RowIndex + 1
End If
End If

End If
If Me.dataGridView1.Columns("name").Index = e.ColumnIndex AndAlso e.RowIndex >= 0 Then
If e.Value.ToString() = Me.dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value.ToString() Then
e.CellStyle.BackColor = Color.LightBlue
nextcol = e.ColumnIndex + 1
End If
End If
End Sub

Private Sub dataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)



'纵向合并
If Me.dataGridView1.Columns("description").Index = e.ColumnIndex AndAlso e.RowIndex >= 0 Then

Using gridBrush As Brush = New SolidBrush(Me.dataGridView1.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor)
Using gridLinePen As New Pen(gridBrush)
' 擦除原单元格背景
e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
'
'''/绘制线条,这些线条是单元格相互间隔的区分线条,
'''/因为我们只对列name做处理,所以datagridview自己会处理左侧和上边缘的线条
If e.RowIndex <> Me.dataGridView1.RowCount - 1 Then
If e.Value.ToString() <> Me.dataGridView1.Rows(e.RowIndex + 1).Cells(e.ColumnIndex).Value.ToString() Then

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
'下边缘的线
'绘制值
If e.Value IsNot Nothing Then
e.Graphics.DrawString(DirectCast(e.Value, String), e.CellStyle.Font, Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y + 2, StringFormat.GenericDefault)
End If
End If
Else
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
'下边缘的线
'绘制值
If e.Value IsNot Nothing Then
e.Graphics.DrawString(DirectCast(e.Value, String), e.CellStyle.Font, Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y + 2, StringFormat.GenericDefault)
End If
End If
'右侧的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)

e.Handled = True
End Using
End Using
End If

'横向合并
If Me.dataGridView1.Columns("name").Index = e.ColumnIndex AndAlso e.RowIndex >= 0 Then

Using gridBrush As Brush = New SolidBrush(Me.dataGridView1.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor)
Using gridLinePen As New Pen(gridBrush)
' 擦除原单元格背景
e.Graphics.FillRectangle(backColorBrush, e.CellBounds)

If e.Value.ToString() <> Me.dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value.ToString() Then

'右侧的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
'绘制值
If e.Value IsNot Nothing Then
e.Graphics.DrawString(DirectCast(e.Value, String), e.CellStyle.Font, Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y + 2, StringFormat.GenericDefault)
End If
End If

'下边缘的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
e.Handled = True
End Using
End Using

End If

End Sub
gouhuafeng 2008-11-15
  • 打赏
  • 举报
回复
Private nextrow As System.Nullable(Of Integer) = Nothing
Private nextcol As System.Nullable(Of Integer) = Nothing
Private Sub dataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs)
If Me.dataGridView1.Columns("description").Index = e.ColumnIndex AndAlso e.RowIndex >= 0 Then
If Me.nextcol IsNot Nothing And e.ColumnIndex = Me.nextcol Then
e.CellStyle.BackColor = Color.LightBlue
Me.nextcol = Nothing
End If
If Me.nextrow IsNot Nothing And e.RowIndex = nextrow Then
e.CellStyle.BackColor = Color.LightPink
Me.nextrow = Nothing
End If
If e.RowIndex <> Me.dataGridView1.RowCount - 1 Then
If e.Value.ToString() = Me.dataGridView1.Rows(e.RowIndex + 1).Cells(e.ColumnIndex).Value.ToString() Then
e.CellStyle.BackColor = Color.LightPink
nextrow = e.RowIndex + 1
End If
End If

End If
If Me.dataGridView1.Columns("name").Index = e.ColumnIndex AndAlso e.RowIndex >= 0 Then
If e.Value.ToString() = Me.dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value.ToString() Then
e.CellStyle.BackColor = Color.LightBlue
nextcol = e.ColumnIndex + 1
End If
End If
End Sub
wuyq11 2008-11-15
  • 打赏
  • 举报
回复
private void CGrid(int y)
{
TableCell oldTc = Gridview1.Rows[0].Cells[y];
for (int i = 1; i < Gridview1.Rows.Count; i++)
{
TableCell tc = Gridview1.Rows[i].Cells[y]; //Cells[y]就是你要合并的列
if (oldTc.Text == tc.Text)
{
tc.Visible = false;
if (oldTc.RowSpan == 0)
{
oldTc.RowSpan = 1;
}
oldTc.RowSpan++;
oldTc.VerticalAlign = VerticalAlign.Middle;
}
else
{
oldTc = tc;
}
}
}
floadcloud 2008-11-15
  • 打赏
  • 举报
回复
   private int? nextrow = null;
private int? nextcol = null;
private void dataGridView1_CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
if (this.dataGridView1.Columns["description"].Index == e.ColumnIndex && e.RowIndex >= 0)
{
if (this.nextcol != null & e.ColumnIndex == this.nextcol)
{
e.CellStyle.BackColor = Color.LightBlue;
this.nextcol = null;
}
if (this.nextrow != null & e.RowIndex == nextrow)
{
e.CellStyle.BackColor = Color.LightPink;
this.nextrow = null;
}
if (e.RowIndex != this.dataGridView1.RowCount - 1)
{
if (e.Value.ToString() == this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
{
e.CellStyle.BackColor = Color.LightPink;
nextrow = e.RowIndex + 1;
}
}

}
if (this.dataGridView1.Columns["name"].Index == e.ColumnIndex && e.RowIndex >= 0)
{
if (e.Value.ToString() == this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())
{
e.CellStyle.BackColor = Color.LightBlue;
nextcol = e.ColumnIndex + 1;
}
}
}
//==========================

//绘制单元格
private void dataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
{



//纵向合并
if (this.dataGridView1.Columns["description"].Index == e.ColumnIndex && e.RowIndex >= 0)
{

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);
/**/////绘制线条,这些线条是单元格相互间隔的区分线条,
////因为我们只对列name做处理,所以datagridview自己会处理左侧和上边缘的线条
if (e.RowIndex != this.dataGridView1.RowCount - 1)
{
if (e.Value.ToString() != this.dataGridView1.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);//下边缘的线
//绘制值
if (e.Value != null)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X + 2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
}
}
else
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线
//绘制值
if (e.Value != null)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X + 2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
}
//右侧的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);

e.Handled = true;
}
}
}

//横向合并
if (this.dataGridView1.Columns["name"].Index == e.ColumnIndex && e.RowIndex >= 0)
{

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.Value.ToString() != this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())
{

//右侧的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
//绘制值
if (e.Value != null)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X + 2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
}

//下边缘的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
e.Handled = true;
}
}

}

}
【公农双历查询】:生多功能日历,可以查询所有节、假日和农历 【高级定位】:多功能选择(查找)工具。可以选择大于某值或者小于某值或者在某范围之间的值,文本定位时支持通配符。还可以按格式查找/定位 【背景着色】:将当前或者进行颜色标示,以突出显示,有利于数据查看。可以随心所欲地定义颜色,还可以自由调整颜色的深浅。本工具相对于同类工具有不破坏背景色、条件格式、复制粘贴和撤消功能之优点 【环境设置】:设置工作表界面视图,控制各项目的显示与隐藏 【隐藏任意磁盘】:将磁盘隐藏,保所隐私文件 【按颜色汇总】:按背景色对选区的数据合类合计 【解除密码保护】:删除工作表保护的密码,瞬间完。适用于Excel 2002、2003、2007、2010 【显示经典菜单】:在Excel 2007或者2010显示2003样式的菜单,方便初学Excel 2007或2010的用户查找菜单 【截取窗口对象】:将选定的窗口或者对象转换图片文件,可截Excel界面和其它任意界面 【截取矩形对象】:将选定的矩形区域转换图片文件 【截取任意区域】:将选定任意区域转换图片文件 【删除空单元格】:删除选区的空单元格,后面的数据自动上升 【转置选区】:将选区行调换 【按倒置】:将选区的数据横向倒置 【按倒置】:将选区的数据纵向倒置 【字母大小写转换】:将选区的单词、字母在大写小写、首字母大写之间转换 【英翻译】:将选区进行英互译,如果选择英文则转换文;如果选择文则转换英文 【小写金额转大写】:将小写金额批量转换大写 【大写金额转小写】:将大写金额批量转换小写 【区域数据加密】:对工作表选区的数据进行加密,转换乱码,有密码才可以查看。 【简体转繁体】:将简体字批量转换繁体 【繁体转简体】:将繁体字批量转换简体 【将公式转换值】:将指定工作表指定区域的公式转换值,可以自由选择工作表和单元格范围。 【根据工资计算钞票】:根据员工的工资计算需要多少张100元、50元......1元的钞票,可以批量计算。发现金工资的财务工作者必备 【隔行插入行】:对工作表隔行插入行,或者隔插入,其行数可以自定义 【折分工作簿】:将指定工作簿的每个工作表拆分单独的工作簿,新工作簿名称等于原工作表名称 【工作表折分】:将当前工作表的数据按条件拆分多个工作表,可以用任意的数据做为拆分条件 【合并工作簿】:将指文件夹所有工作簿所有工作表数据合并起来。有两种合并方式:将每个工作簿的工作表合到当前工作簿是,表与表对应;将不同工作簿同工作表的数据合并同一工作表。差异在于同名工作表的处理 【文本与数值互换】:将选区的数字瞬间转换文本;将选区的文本型数字瞬间转换数值 【复选框工具】:批量生复选框(方框打勾的工具),批量选定、取消复选框。且可以定义复框是否可以打印、与单元格链接等等 【报表分栏工具箱】:Word有分栏功能,本工具使Excel也具有同类功能。当数据数太少浪费打印纸张时,可以用本工具分多栏再打印 【制作工资条】:瞬间将工资明细表生工资条,方便打印并裁剪。可以自己定义工资条头的行数 【删除工资条恢复明细表】:删除前一工具生的工资条头,恢复明细表 【建立分页小计】:将每页数据建立小计和累计,且自动分页,小计和累计在每页最末处 【删除分页小计】:删除小计与累计,恢复明细表 【合并到选区】:将一个单元格的值合到一个区域去,可以插入到原字符之前也可以插入到原字符之后 【可还原的合并】:合并数据时可以保留所有数据,可以随心所欲定义分隔符。还可以随时取消合并,还原所有数据 【合并相同值】:对一相同且相邻的数据区域进行合并 【取消合并还原合并数据】:对一合并后的单元格取消合并,且恢复合并前所有数据 【合并数据并复制】:可以将一个区域的数据直接复制到一个单元格合并区域自动换行】:可以让具有合并单元格的区域在自动换行与不换行之间自由切换

16,720

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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