为了表示行号,我们可以在DataGridView的RowPostPaint事件中进行绘制。RowPostPaint事件,具体可以参照MSDN。
下面是实现代码:
c# 实现:
private void Form1_Load(object sender, System.EventArgs e)
{
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.DataSource = System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders();
}
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(e.RowBounds.Location.X,
e.RowBounds.Location.Y,
dataGridView1.RowHeadersWidth - 4,
e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
dataGridView1.RowHeadersDefaultCellStyle.Font,
rectangle,
dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}
vb.net 实现:
Private Sub dgvlist_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dgvlist.RowPostPaint
Dim rectangle As System.Drawing.Rectangle = New System.Drawing.Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgvlist.RowHeadersWidth - 4, e.RowBounds.Height)
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvlist.RowHeadersDefaultCellStyle.Font, rectangle, dgvlist.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter + TextFormatFlags.Right)
End Sub
<DllImport("coredll.dll")> _
Public Shared Function SendMessage(ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer)
End Function
<DllImport("coredll.dll")> _
Public Shared Function PostMessage(ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer)
End Function
Me.DGUI = New System.Windows.Forms.DataGrid
'''''''''''内存数据表的建立'''''''''''''''''' Dim dset As DataSet = New DataSet("memdset") Dim dtab As DataTable = dset.Tables.Add("UI") dtab.Columns.Add("相别", GetType(String)) 'New String("").GetType
dtab.Columns.Add("测量值", GetType(String))
dtab.Columns.Add("相位", GetType(String))
DRow = dtab.NewRow ' DRow("相别") = "Ua" DRow("测量值") = "220.3" DRow("相位") = "653.1256" dtab.Rows.Add(DRow) DRow = dtab.NewRow '
DRow("相别") = "Ub"
DRow("测量值") = "230.3"
DRow("相位") = "3.1256"
dtab.Rows.Add(DRow)
DRow = dtab.NewRow ' DRow("相别") = "Uc" DRow("测量值") = "0.3" DRow("相位") = "120.56" dtab.Rows.Add(DRow) DGUI.DataSource = dtab DGUI.BackColor = Color.Azure DGUI.BackgroundColor = Color.White '''''''''''DataGrid控件列宽自动控制方法''''''''''''''''' Dim dgtype As DataGridTableStyle = New DataGridTableStyle dgtype.MappingName = dtab.TableName '这一步不可缺少
DGUI.TableStyles.Clear()
DGUI.TableStyles.Add(dgtype)
Dim dgcoltype As GridColumnStylesCollection = DGUI.TableStyles(0).GridColumnStyles
dgcoltype(0).Width = 50
dgcoltype(1).Width = 100
dgcoltype(2).Width = 100
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''DataGrid控件上绘图方法,该绘图方法可行,但是由于DataGrid本身有数据,导致本函数退出后DataGrid自身的刷新会覆盖下面的绘图。 SetGridLines(DGUI) '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''' ''' <summary>
''' DataGrid上绘图,测试发现listview不可实现该功能 ''' </summary>
''' <param name="lvw"></param> ''' <remarks></remarks>
Public Shared Sub SetGridLines(ByVal lvw As System.Windows.Forms.DataGrid)
Try
' Create image. Dim drawFont As New System.Drawing.Font("Arial", 10, FontStyle.Regular) Dim drawBrush As New _ System.Drawing.SolidBrush(System.Drawing.Color.Black) '
'绘制坐标系 '
''''''''''''''''''''''''Ua 始终在度方向''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim newGraphics As Graphics = lvw.CreateGraphics
'newGraphics.Clear(Color.White) 'newGraphics.Clear(System.Drawing.Color.FromArgb(CType(CType(224, Byte), Integer), CType(CType(224, Byte), Integer), CType(CType(224, Byte), Integer)))
Dim x As Integer = newGraphics.DpiX
Dim y As Integer = newGraphics.DpiY
Dim R As Integer = 100
Dim pen As Pen = New Pen(Color.Black, 1)
newGraphics.DrawEllipse(pen, 0, 0, 2 * R, 2 * R)
newGraphics.DrawLine(pen, R, 0, R, 2 * R)
newGraphics.DrawLine(pen, 0, R, 2 * R, R)
' '计算角度指向的终点坐标
' Dim tpoint As Point() = {New Point(10, 10), New Point(30, 30)} Dim clor() As System.Drawing.Color = {Color.Gold, Color.Green, Color.Red} newGraphics.DrawString("test", drawFont, drawBrush, tpoint(1).X, tpoint(1).Y) drawFont.Dispose() drawBrush.Dispose() newGraphics.Dispose() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub
''' <summary>
''' 控件动态添加函数,向FlowLayoutPanel控件flp1中动态添加子控件
''' </summary>
''' <param name="text"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function flp1_add(ByVal text As String) As Boolean
If text.ToLower.StartsWith("system") Or String.IsNullOrEmpty(text.Trim) Then
Return False
Exit Function
End If
For i As Int16 = 0 To flp1.Controls.Count - 1
If flp1.Controls.Item(i).Text = text Then
RemoveHandler flp1.Controls.Item(i).MouseDoubleClick, AddressOf labclicked_DoubleClick '指定子控件鼠标双击响应函数
flp1.Controls.RemoveAt(i)
Return False
Exit Function
End If
Next
Dim netctl As Control = New Label
netctl.Text = text
netctl.BackColor = Color.GreenYellow
flp1.Controls.Add(netctl)
AddHandler netctl.MouseDoubleClick, AddressOf labclicked_DoubleClick '指定子控件鼠标双击响应函数
Return True
End Function
''' <summary>
''' 子控件鼠标双击响应函数
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Sub labclicked_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim lab As Label = sender
Dim str As String = lab.Text
flp1_add(str)
'厂家
Dim x As Int16 = InStr(selectcond1, str)
If x > 0 Then
selectcond1 = selectcond1.Remove(x - 1, str.Length + 1)
If String.IsNullOrEmpty(selectcond1) Then
cbCJ.Checked = False
cobCJ.Text = ""
End If
End If
'工程队
x = InStr(selectcond2, str)
If x > 0 Then
selectcond2 = selectcond2.Remove(x - 1, str.Length + 1)
If String.IsNullOrEmpty(selectcond2) Then
cbGCD.Checked = False
cobGCD.Text = ""
End If
End If
'浮动标志
x = InStr(str, "浮动:")
If x > 0 Then
selectcond3 = ""
cbFDBZ.Checked = False
cobFDBZ.Text = ""
End If
'出账
x = InStr(str, "出账:")
If x > 0 Then
selectcond4 = ""
cbCZ.Checked = False
cobCZ.Text = ""
End If
End Sub