在DataGrid中將RowHeader 加上文字...
1、新建一個工程…
2、在工程內添加一個Form….
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
3?、在Form 上建一拖建一個DataGrid 和一個 Button....
代碼部分:
?
Imports System.Drawing.Graphics
Imports System.Drawing.Font
Imports System.Drawing
Public Class Form1
??? Inherits System.Windows.Forms.Form
?
#Region " Windows Form Designer generated code "
?
??? Public Sub New()
??????? MyBase.New()
?
??????? 'This call is required by the Windows Form Designer.
??????? InitializeComponent()
?
??????? 'Add any initialization after the InitializeComponent() call
?
??? End Sub
?
??? 'Form overrides dispose to clean up the component list.
??? Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
??????? If disposing Then
??????????? If Not (components Is Nothing) Then
??????????????? components.Dispose()
??????????? End If
??????? End If
??????? MyBase.Dispose(disposing)
??? End Sub
?
??? 'Required by the Windows Form Designer
??? Private components As System.ComponentModel.IContainer
?
??? 'NOTE: The following procedure is required by the Windows Form Designer
??? 'It can be modified using the Windows Form Designer.?
??? 'Do not modify it using the code editor.
??? Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
??? Friend WithEvents Button1 As System.Windows.Forms.Button
??? <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
??????? Me.DataGrid1 = New System.Windows.Forms.DataGrid
??????? Me.Button1 = New System.Windows.Forms.Button
??????? CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
??????? Me.SuspendLayout()
??????? '
??????? 'DataGrid1
??????? '
??????? Me.DataGrid1.DataMember = ""
??????? Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
??????? Me.DataGrid1.Location = New System.Drawing.Point(2, 0)
??????? Me.DataGrid1.Name = "DataGrid1"
??????? Me.DataGrid1.Size = New System.Drawing.Size(660, 316)
??????? Me.DataGrid1.TabIndex = 0
??????? '
??????? 'Button1
??????? '
??????? Me.Button1.Location = New System.Drawing.Point(578, 330)
??????? Me.Button1.Name = "Button1"
??????? Me.Button1.TabIndex = 1
??????? Me.Button1.Text = "Button1"
??????? '
??????? 'Form1
??????? '
??????? Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
??????? Me.ClientSize = New System.Drawing.Size(676, 361)
??????? Me.Controls.Add(Me.Button1)
??????? Me.Controls.Add(Me.DataGrid1)
??????? Me.Name = "Form1"
??????? Me.Text = "Form1"
??????? CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
??????? Me.ResumeLayout(False)
?
??? End Sub
?
#End Region
?
??? Private idtb_temp As New DataTable
??? Private idrw_rows As DataRow
?
?
??? Private str_conn As String = "provider=microsoft.jet.oledb.4.0;User Id=admin;Password=;data source="
??? Private Function SetDataGridStyle(ByVal adtb_data_source As DataTable) As DataGridTableStyle
??????? Dim lint_col_current As Integer = 0
??????? Dim lint_col_count As Integer
?
??????? Dim lsty_temp_datagrid As New DataGridTableStyle
??????? lsty_temp_datagrid.MappingName = adtb_data_source.TableName
??????? Dim lcol_columns_text As DataGridTextBoxColumn
??????? Dim larr_columnsname(2) As String
??????? Dim larr_columnswidth(2) As Integer
??????? larr_columnsname(0) = "第一列"
??????? larr_columnswidth(0) = "70"
??????? larr_columnsname(1) = "第二列"
??????? larr_columnswidth(1) = "170"
??????? larr_columnsname(2) = "第三列"
??????? larr_columnswidth(2) = "270"
?
??????? lint_col_count = adtb_data_source.Columns.Count
??????? Do While (lint_col_current < lint_col_count)
?
??????????? lcol_columns_text = New DataGridTextBoxColumn
??????????? lcol_columns_text.MappingName = adtb_data_source.Columns(lint_col_current).ColumnName
??????????? lcol_columns_text.HeaderText = larr_columnsname(lint_col_current)
??????????? lcol_columns_text.Width = larr_columnswidth(lint_col_current)
??????????? lcol_columns_text.TextBox.AutoSize = True
?
??????????? lsty_temp_datagrid.GridColumnStyles.Add(lcol_columns_text)
?
??????????? lint_col_current = lint_col_current + 1
?
??????? <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Loop
??????? 'lsty_temp_datagrid.HeaderBackColor = System.Drawing.Color.Plum
??????? 'lsty_temp_datagrid.GridLineColor = System.Drawing.Color.Red
?
??????? 'lsty_temp_datagrid.SelectionBackColor = System.Drawing.Color.Green
??????? lsty_temp_datagrid.RowHeaderWidth = 120
??????? 'lsty_temp_datagrid.RowHeadersVisible = False
?
??????? 'lsty_temp_datagrid.BackColor = System.Drawing.Color.Gold
??????? 'lsty_temp_datagrid.AlternatingBackColor = System.Drawing.Color.Red
?
??????? Return lsty_temp_datagrid
??? End Function
?
??? Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
??????? Me.DataGrid1.DataSource = idtb_temp
?
??????? Me.DataGrid1.TableStyles.Clear()
??????? Me.DataGrid1.TableStyles.Add(Me.SetDataGridStyle(idtb_temp))
?
??? End Sub
?
??? Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
??????? Dim fileinfo As New IO.FileInfo(Application.ExecutablePath)
??????? str_conn &= fileinfo.DirectoryName & "/datagrid_sort.mdb"
??????? Dim str_sql As String = "select * from sort"
??????? Dim objconn As New OleDb.OleDbConnection(str_conn)
??????? objconn.Open()
??????? Dim objda As New OleDb.OleDbDataAdapter(str_sql, objconn)
??????? objda.Fill(idtb_temp)
?
?
??? End Sub
?
??? Private Sub DrawRowHeader(ByVal dg As DataGrid)
??????? Dim g As Graphics
??????? g = Me.DataGrid1.CreateGraphics
??????? If Me.DesignMode Then Exit Sub
??????? Try
?
??????????? Dim RowCount As Integer
?
??????????? If dg.DataSource Is Nothing Then
??????????????? RowCount = -1
??????????? Else
??????????????? RowCount = dg.BindingContext(dg.DataSource, dg.DataMember).Count
??????????? End If
?
??????????? If RowCount > 0 Then
?
??????????????? Dim i As Integer
??????? ????????Dim intTop As Integer
??????????????? Dim intFirstRow As Integer
??????????????? Dim intFirstRowTop As Integer
??????????????? Dim NumberWidth As Integer
?
??????????????? Dim blnNoTableStyle As Boolean
??????????????? NumberWidth = CInt(g.MeasureString(RowCount.ToString, dg.Font).Width) + 15
??????????????? blnNoTableStyle = (dg.TableStyles.Count = 0)
?
??????????????? If blnNoTableStyle Then
??????????????????? dg.RowHeaderWidth = NumberWidth
?? ?????????????ElseIf NumberWidth > dg.TableStyles(0).RowHeaderWidth Then
??????????????????? dg.TableStyles(0).RowHeaderWidth = NumberWidth
??????????????? End If
?
??????????????? intFirstRowTop = dg.GetCellBounds(0, 0).Top
??????????????? '''
???????????? ???intFirstRow = CInt(intFirstRowTop / ((dg.GetCellBounds(RowCount - 1, 0).Top - intFirstRowTop + dg.GetCellBounds(0, 0).Height) / RowCount))
??????????????? '''
??????????????? intFirstRow = intFirstRow - CInt(IIf(False, 1, IIf(dg.CaptionVisible = True, 2, 1)))
??????????????? If intFirstRow < 0 Then
??????????????????? intFirstRow = -intFirstRow
??????????????? Else
??????????????????? intFirstRow = 0
??????????????? End If
?
??????????????? For i = intFirstRow To RowCount - 1
??????????????????? intTop = dg.GetCellBounds(i, 0).Top + 2
??????????????????? g.DrawString("第 " & CStr(i + 1) & " 行", dg.Font, New SolidBrush(Color.Red), 15, intTop)
??????????????? Next
?
??????????? End If
?
??????? Catch ex As Exception
?
??????? End Try
??? End Sub
?
??? Private Sub DataGrid1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGrid1.Paint
??????? DrawRowHeader(Me.DataGrid1)
??? End Sub
End Class
?
?效果圖:
轉載于:https://www.cnblogs.com/sesexxoo/archive/2005/02/16/6190418.html
總結
以上是生活随笔為你收集整理的在DataGrid中將RowHeader 加上文字...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: My MPC系列·暴风影音 V5.00
- 下一篇: 指南--安装 NVU HTML 编辑器