Plus RowHeader in DataGrid ...

xiaoxiao2021-03-06  41

1. Create a new project ...

2, add a form to the project ....

3, built a DataGrid and a button on Form and a Button ....

Code section:

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 anyinitization 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

private subinitializecomponent ()

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 dataable) AS DataGridtableLelele

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) = "first column"

LARR_COLUMNSWIDTH (0) = "70"

LARR_COLUMNSNAME (1) = "second column"

LARR_COLUMNSWIDTH (1) = "170"

LARR_COLUMNSNAME (2) = "Third Column"

LARR_COLUMNSWIDTH (2) = "270"

LINT_COL_COUNT = ADTB_DATA_SOURCE.COLUMNS.COUNT

Do while (lint_col_current

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

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.rtowHeadersvisible = FALSE

'LSty_Temp_DataGrid.backcolor = system.drawing.color.gold

'LSty_Temp_DataGrid.alternatingbackbackcolor = system.drawing.color.red

Return LSty_Temp_DataGrid

END FUNCTION

Private sub button1_click (byvale as system.object, byval e as system.eventargs) Handles Button1.click

Me.DataGrid1.datasource = IDTB_TEMP

Me.DataGrid1.tablestyles.clear ()

Me.DataGrid1.tables.add (me.setdataGridStyle (idtb_temp))

End Sub

Private Sub Form1_Load (Byval E 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) .countendiff

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.toToString, Dg.Font) .Width) 15

BLNNOTABLESTYLE = (Dg.TablesTyles.count = 0)

IF blnnotablestyle Then

Dg.RowHeaderWidth = Numberwidth

Elseif Numberwidth> Dg.Tables (0). RowHeaderwidth Then

Dg.TablesTyles (0) .rtowheaderwidth = 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) & "line", DG.Font, New Solidbrush (Color.Red), 15, INTTOP)

NEXT

END IF

Catch exception

END TRY

End Sub

Private sub DataGrid1_paint (Byval e as system.windows.forms.painteventargs) Handles DataGrid1.paint

DrawRowHeader (Me.DataGrid1)

End Sub

END CLASS

Rendering:

转载请注明原文地址:https://www.9cbs.com/read-57654.html

New Post(0)