不确定在此搜索什么 在下面列出的绘制事件代码中,它绘制网格线以填充datagridview的空白空间,而不必添加空行 这段代码确实有效,但我仍然在制作mod 我想知道如何做的是使事件成为我所有datagridviews的一部分,而不必重复和修改代码 例如,我有8个可以使用它的datagridviews我不希望在我的代码中有8个这样的副本,最好的方法是在每个datagridview上设置gridline属性,如果它是true,那么使用下面的用于绘制线条的代码
但是就目前而言,我必须将此代码作为每个datagridview的事件
我不想为我编写代码但需要建议从哪里开始
Private Sub DataGridView1_Paint(sender As Object, e As PaintEventArgs) Handles DataGridView1.Paint
Dim intRowHeight As Integer
Dim intTopOffset As Integer
Dim intLeftOffset As Integer
Dim objRowImage As System.Drawing.Bitmap
Dim objGraphics As System.Drawing.Graphics
Dim objRowFrame As System.Drawing.Rectangle
Dim objRowFill As System.Drawing.Rectangle
Dim objRowHeaderFrame As System.Drawing.Rectangle
Dim intIndex As Integer
Dim intLastIndex As Integer
Dim intRowHeadersWidth As Integer
If DataGridView1.AllowUserToAddRows Then
intLastIndex = DataGridView1.NewRowIndex
Else
intLastIndex = DataGridView1.Rows.Count - 1
End If
If DataGridView1.RowHeadersVisible Then
intRowHeadersWidth = DataGridView1.RowHeadersWidth
Else
intRowHeadersWidth = 1
End If
intRowHeight = DataGridView1.RowTemplate.Height
intTopOffset = DataGridView1.ColumnHeadersHeight + intRowHeight * (intLastIndex + 1)
If DataGridView1.Height - intTopOffset > intRowHeight Then
objRowImage = New System.Drawing.Bitmap(DataGridView1.Width, intRowHeight)
objGraphics = System.Drawing.Graphics.FromImage(objRowImage)
objRowFrame = New System.Drawing.Rectangle(0, 0, DataGridView1.Width, intRowHeight)
objGraphics.DrawRectangle(System.Drawing.SystemPens.ControlDark, objRowFrame)
objRowFill = New System.Drawing.Rectangle(1, 1, DataGridView1.Width - 2, intRowHeight - 2)
objGraphics.FillRectangle(System.Drawing.SystemBrushes.Window, objRowFill)
objRowHeaderFrame = New System.Drawing.Rectangle(2, 2, intRowHeadersWidth - 2, intRowHeight - 4)
objGraphics.FillRectangle(New System.Drawing.SolidBrush(DataGridView1.RowHeadersDefaultCellStyle.BackColor), objRowHeaderFrame)
intLeftOffset = intRowHeadersWidth
For Each objColumn As System.Windows.Forms.DataGridViewColumn In DataGridView1.Columns
If objColumn.Visible Then
intLeftOffset += objColumn.Width
objGraphics.DrawLine(System.Drawing.SystemPens.ControlDark, New System.Drawing.Point(intLeftOffset, 0), New System.Drawing.Point(intLeftOffset, intRowHeight))
End If
Next objColumn
intIndex = (DataGridView1.Height - intTopOffset) \ intRowHeight
For j As Integer = 0 To intIndex
e.Graphics.DrawImage(objRowImage, 0, intTopOffset + j * intRowHeight)
Next j
End If
End Sub
答案 0 :(得分:1)
首先,在+oo
关键字后添加所有DataGridView:
Handles
接下来,将Private Sub DataGridView1_Paint(sender As Object, e As PaintEventArgs) _
Handles DataGridView1.Paint, DataGridView2.Paint, DataGridView3.Paint, etc...
参数强制转换为DataGridView:
sender
最后更改所有代码以使用 Dim DG As DataGridView = DirectCast(sender, DataGridView)
代替DG
:
DataGridView1
答案 1 :(得分:1)
只需创建自己的datagridview版本。
创建一个新类,继承Datagridview,为paint事件添加一些功能加上新属性,重建受影响的项目并使用你的新类,就好像原来有一些额外的功能......因为它是。< / p>
Public Class mdgv
Inherits DataGridView
Property doit As Boolean = False
Private Sub mdgv_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
If doit = True Then
MessageBox.Show("painted")
End If
End Sub
End Class
然后,用新类MDGV替换当前的DATAGRIDVIEW对象。希望这会有所帮助。:)
答案 2 :(得分:0)
首先让你在某个类中使用方法Shared
或将其放在Module
中
在此之后,您的事件处理程序可以在项目中以不同的形式使用
然后正如@idle_Mind所提到的,将sender
对象强制转换为DataGridView
类型并在事件处理程序中使用
Public Module YourSomeModule
Public Sub DataGridView_MyHandlerPaint(sender As Object, e As PaintEventArgs)
'Cast instance of the DataGridView from "sender" object
'Name it same as in your code,
'then you don't need to change name in the code
'(If you not used "Me.DataGridView", if so just remove "Me.")
Dim DataGridView1 As DataGridView = DirectCast(sender, DataGridView)
'Here your code
End Sub
End Public
然后只需要做的事情 - 在表单中的datagridview的Paint
事件中添加处理程序。
在Me.Initialize()
方法
Public Class MyForm1
Public Sub New()
Me.Initialize()
AddHandler Me.DataGridView1.Paint,
AddressOf YourSomeModule.DataGridView_MyHandlerPaint
End Sub
End Class
另一个表格
Public Class MyForm2
Public Sub New()
Me.Initialize()
AddHandler Me.DataGridView2.Paint,
AddressOf YourSomeModule.DataGridView_MyHandlerPaint
End Sub
End Class