none
Visual Basic 2010 DataGridview中 两个单元格间能连线(就像Excel画线功能)吗? RRS feed

  • 问题

  • Visual Basic 2010 DataGridview中 两个单元格间能连线(就像Excel画线功能)吗?

    谢谢。

    要代码。

    2011年3月21日 7:51

答案

  • 你好,

    你可以在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.

    2011年3月22日 3:58
  • 设置创建一个新的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.

    2011年3月26日 16:14
  • 谢谢.

    能运用了.

    • 已标记为答案 蓝天712 2011年3月27日 1:18
    2011年3月27日 1:17

全部回复

  • 你好,

    你可以在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.

    2011年3月22日 3:58
  • 非常感谢

    能运行了

    还有个问题,线条粗细怎么处理

    谢谢

    2011年3月22日 12:16
  • 设置创建一个新的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.

    2011年3月26日 16:14
  • 谢谢.

    能运用了.

    • 已标记为答案 蓝天712 2011年3月27日 1:18
    2011年3月27日 1:17