积极答复者
Visual Basic 2010 DataGridview中 两个单元格间能连线(就像Excel画线功能)吗?

问题
答案
-
你好,
你可以在DataGridView_Paint事件中画线。请尝试下面这段代码,它实现了在第c1行r1列和c2行r2列两单元格之间画线的功能。
Private Sub DataGridView1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGridView1.Paint Dim c1, r1 As Integer '起点单元格的行和列 Dim c2, r2 As Integer '终点单元格的航和列 If DataGridView1.Columns(c1).Visible And DataGridView1.Rows(r1).Visible And DataGridView1.Columns(c2).Visible And DataGridView1.Rows(r2).Visible Then Dim p As Pen = New Pen(Color.Red) Dim rectCell1 As Rectangle, rectCell2 As Rectangle rectCell1 = DataGridView1.GetCellDisplayRectangle(c1, r1, True) rectCell2 = DataGridView1.GetCellDisplayRectangle(c2, r2, True) Dim lineFrom As Point lineFrom.X = rectCell1.X lineFrom.Y = rectCell1.Y + rectCell1.Height / 2 Dim lineTo As Point lineTo.X = rectCell2.X + rectCell2.Width lineTo.Y = rectCell2.Y + rectCell2.Height / 2 e.Graphics.DrawLine(p, rectCell1.X, rectCell1.Y, rectCell2.X, rectCell2.Y) End If End Sub
Best regards
Liliane Teng [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Liliane Teng 2011年3月29日 2:42
-
设置创建一个新的Pen,设置Pen.Width,这里Width指示System.Drawing.Pen的宽度的值,显示线条的粗细。
Dim p As Pen = New Pen(Color.Red,5)
Best regards
Liliane Teng [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Liliane Teng 2011年3月29日 2:43
全部回复
-
你好,
你可以在DataGridView_Paint事件中画线。请尝试下面这段代码,它实现了在第c1行r1列和c2行r2列两单元格之间画线的功能。
Private Sub DataGridView1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGridView1.Paint Dim c1, r1 As Integer '起点单元格的行和列 Dim c2, r2 As Integer '终点单元格的航和列 If DataGridView1.Columns(c1).Visible And DataGridView1.Rows(r1).Visible And DataGridView1.Columns(c2).Visible And DataGridView1.Rows(r2).Visible Then Dim p As Pen = New Pen(Color.Red) Dim rectCell1 As Rectangle, rectCell2 As Rectangle rectCell1 = DataGridView1.GetCellDisplayRectangle(c1, r1, True) rectCell2 = DataGridView1.GetCellDisplayRectangle(c2, r2, True) Dim lineFrom As Point lineFrom.X = rectCell1.X lineFrom.Y = rectCell1.Y + rectCell1.Height / 2 Dim lineTo As Point lineTo.X = rectCell2.X + rectCell2.Width lineTo.Y = rectCell2.Y + rectCell2.Height / 2 e.Graphics.DrawLine(p, rectCell1.X, rectCell1.Y, rectCell2.X, rectCell2.Y) End If End Sub
Best regards
Liliane Teng [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Liliane Teng 2011年3月29日 2:42
-
设置创建一个新的Pen,设置Pen.Width,这里Width指示System.Drawing.Pen的宽度的值,显示线条的粗细。
Dim p As Pen = New Pen(Color.Red,5)
Best regards
Liliane Teng [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Liliane Teng 2011年3月29日 2:43