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

renchuang 2008-11-15 12:42:32
datagrivdview中同一列或同一行中相临的单元格中相同是怎样合并成一个单元格
...全文
95 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;
}
}

}

}

16,554

社区成员

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

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