日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

在DataGrid中將RowHeader 加上文字...

發(fā)布時間:2025/3/20 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在DataGrid中將RowHeader 加上文字... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、新建一個工程

2、在工程內(nèi)添加一個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

?

?效果圖:


轉(zhuǎn)載于:https://www.cnblogs.com/sesexxoo/archive/2005/02/16/6190418.html

總結(jié)

以上是生活随笔為你收集整理的在DataGrid中將RowHeader 加上文字...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。