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 加上文字...相关推荐

  1. python自带的PIL库扩展图片大小给图片加上文字描述

    利用python自带的PIL库扩展图片大小给图片加上文字描述.大多都是库函数调用,只是给定图片宽度后计算文字所需行数的代码需要写. 代码比较丑,but it works. #!/usr/bin/env ...

  2. 5分钟在浏览器中直接扫描并识别文字

    简介 在文档电子化的过程中,常常需要从获取的图像文件中提取出需要的信息.这个过程中,最准确的方式是用机器思考的方式进行,典型的做法是识别二维码获取内容.如果您的文档满足这个条件,可以参考[二维条码识别 ...

  3. VUE中icon图标与文字不齐

    VUE中icon图标与文字不齐 最近在使用vant组件引用了某个icon之后发现与文字对不齐,找到了一个最简便的办法就是直接给icon加上属性vertical-align: -10% <van- ...

  4. Android程式码怎么添加按钮,Android,UI 在程式中即時加上其他的UI?|?柯博文 Powen Ko...

    Android,UI 在程式中即時加上其他的UI 0 Comments 柯博文 在下面介紹, 如何在Android UI 在程式中即時加上其他的UI View tmpView; tmpView = i ...

  5. 计算机给文字填充颜色,如何将电脑word文档中新录入的文字设置为不同的颜色...

    如何将电脑word文档中新录入的文字设置为不同的颜色 腾讯视频/爱奇艺/优酷/外卖 充值4折起 当我们在使用电脑的时候,一般都会用到word软件来处理文档,如果想要将新录入的文字设置为与旧文字不同的颜 ...

  6. 计算机中文字底纹咋操作,word文档中的如何添加文字背景? -电脑资料

    格式菜单>>背景 里面选择, ------ 让你的Word文档背景更漂亮 大家知道,在默认设置下,Word文档的背景都是单调的白色.如果你喜欢让它变得更漂亮些,可以采取下面的方法尝试改变背 ...

  7. 在DataGrid中显示图片

    兼谈 DadaGrid 模板列的创建 DadaGrid 是 ASP.NET 编程中一个很重要的控件,其优良的可定制功能为提高它的表现力提供了极大的方便.除了与数据源直接绑定以外,我们还可以通过列绑定模 ...

  8. Springboot中给图片添加文字水印

    Springboot中给图片添加文字水印 工作中遇到给图片添加文字水印的需求,记录下来方便之后查阅 需求内容: 给一张图片添加指定文字水印,使一张图片上有多个水印内容,并且设定一个水印开关,可指定是否 ...

  9. 在DataGrid中添加一个LookUpColumn,以比较灵活地实现DictValue值代替DictID值显示.

    在一个项目中,我遇到这样一个问题: 数据库字段只存储了一个字典ID1,在DataGrid中显示时,需要用相应的字典Value1来代替字典ID1显示.解决这个问题一般有两个方法: 方法1:  数据库查询 ...

最新文章

  1. Android开发之触摸事件处理机制详解
  2. hibernate之工具类
  3. 【转】服务器维护工程师悲惨的一个星期
  4. Vue父组件使用子组件时,需要携带参数,函数内如何获取子组件给的值
  5. python实现单例模式的几种方式_基于Python中单例模式的几种实现方式及优化详解...
  6. Python判断变量是否存在的方法
  7. windows2000 日志
  8. CPNDet:Corner Proposal Network for Anchor-free, Two-stage Object Detection
  9. 基于JavaScript的在线语音识别库Julius
  10. iOS oc 线程 进程,同步异步,并发串行,来捋捋
  11. 计算机师范专业考编,不是师范生考教师很难?这4个专业考教师反而轻松!
  12. qlineargradient线性渐变
  13. CHD-5.3.6集群安装
  14. java word 纸张大小_Java 设置Word页边距、页面大小、页面方向、页面边框
  15. 【RocketMQ 二十七】RocketMQ 消费幂等
  16. 【C++】栈~~(很详细哦)
  17. 跑酷游戏-第13届蓝桥杯Scratch国赛真题第3题
  18. 调用二级页面的html代码,从一级页面直接调用二级页面中的iframe框架
  19. xgboost:防止过拟合的方法收缩和特征列、采样
  20. 如何多人协作编辑文档

热门文章

  1. JMS学习(五)--ActiveMQ中的消息的持久化和非持久化 以及 持久订阅者 和 非持久订阅者之间的区别与联系...
  2. sysbench 一些选项参数记录
  3. Spring PropertyPlaceholderConfigurer Usage
  4. 由浅至深,谈谈.NET混淆原理 -- 五(MaxtoCode原理),六(其它保护方法)
  5. 听说这里有让你膜拜的产品方法论?
  6. 产品设计中的点线面法则
  7. 致北漂——你来北京不是为了配合出演苦情戏
  8. 这八大互联网金融商业模式,你都知道吗?
  9. PMCAFF | 史上最完整的沙龙活动策划总结
  10. 【行业】点餐App未来必须面对的三大难题