Henry Instruction: Winform DataGrid Structure Analysis (3) Using Code

zhaozj2021-02-16  51

Connected (3) Class code: (due to the limit of the number of words per article, I have to develop, forgive me!)

'------------------------------------- ---------------------

'Auxiliary method

'------------------------------------- ---------------------

Private Readonly Property DataGridTableGridLineWidth () AS Integer

Get

If Me.DataGridtablesTyle.GridLineStyle = DataGridLineStyle.solid THEN

Return 1

Else

Return 0

END IF

END GET

End Property

Private sub eyesidit ()

INEDIT = FALSE

Invalidate ()

End Sub

Private function gettext (byval value as object) AS String

IF value is system.dbnull.value the return nulltext

IF not value is nothing then

Return Value.toString

Else

Return String.empty

END IF

END FUNCTION

Private sub hidecomboBox () Hide ComboBox

IF combo.focused the

Me.DataGridtablestyle.DataGrid.foCus ()

END IF

Combo.visible = false

End Sub

Private sub rollback ()

Combo.Text = OldVal

End Sub

Private sub painttext (Byval g as graphics, _

Byval Bounds as Rectangle, _

Byval text as string, _

Byval aligntoright as boolean)

DIM BACKBRUSH AS BRUSH = New Solidbrush (Me.DataGridtablestyle.backcolor)

Dim forebrush as brush = new solidbrush (me.datagridtablestyle.ForeColor)

PaintText (G, Bounds, Text, Backbrush, Forebrush, Aligntoright)

End Sub

Private sub painttext (Byval g as graphics, _

Byval TextBounds as Rectangle, _

Byval text as string, _

Byval backbrush as brush, _

Byval forebrush as brush, _

Byval aligntoright as boolean)

Dim Rect As Rectangle = TextBounds

DIM Rectf as Rectanglef = RectF.op_implicit (rect) to Rectanglef Type

DIM FORMAT AS STRINGFORMAT = New StringFormat ()

IF aligntoright kil

Format.formatflags = stringformatflags.directionrightttoleft

End ifselect case me.alignment

Case is = horizontalalignment.Left

Format.Alignment = stringalignment.near

Case is = horizontalignment.right

Format.Alignment = stringalignment.far

Case is = horizontalalight.center

Format.Alignment = stringalignment.center

End SELECT

Format.formatflags = format.formatflags or stringFormatflags.nowrap

G.FillRectangle (brush: = backbrush, reference: = rect)

Rect.offset (0, ymargin)

Rect.height - = ymargin

g.drawstring (Text, Me.DataGridtablesTyle.DataGrid.font, forebrush, restf, format)

Format.dispose ()

End Sub

END CLASS

4. Use of class

How to use this class? The method is actually very simple, it looks like DataGridTextBoxColumn or DataGridBoolColumn. See here: Database name is northwind.mdb

Private subfrmdropdowncolumn_load (Byval e as system.eventargs) Handles mybase.load

Dim ObjcuStomercm As CurrencyManager

DIM ObjcuStomertablesTyle As DataGridTableStyle

Dim Objgridcol as DataGridColumnstyle

DIM INTAVGCHARWIDTH AS INTEGER

'New a DataSet object

_CUSTOMERDS = New Dataset ()

'New a DataTable object

_STATESDT = New DataTable ()

'New a DataGridTableStyle object

GridtableStyle = new dataGridtableStyle ()

'Calculate the width of each character

With graphics.fromhwnd (me.handle) .measureString (Text: = "AbcdefghijklmnopqrStuvwxyz", font: = me.font)

INTAVGCHARWIDTH = CINT (.width / 26!)

End with

'Establish connections to your database

_Db = new oledbconnection ()

Try

With _db

.Connectionstring = "provider = microsoft.jet.Oledb.4.0;" & _

"Data Source = C: /NorthWind.mdb"

.Open ()

End with

Catch DBERROR AS OLEDBEXCEPTION

STOP

END TRY

_Db.close ()

'

'Newly generated a SQL statement and newly built a user table in DataSet

STRSQL = "SELECT * FROM Customer WHERE National =" & Chr (34) & "US" & Chr (34) & "Order By Customer ID ASC" TRY

With new oledbdataadapter (selectcommand: = new oledbcommand (cmdtext: = strsql, _

Connection: = _ dB))

.Fill (DataSet: = _ CustomerDS, SRCTABLE: = "CUSTOMERS")

.Dispose ()

End with

Catch DBERROR AS OLEDBEXCEPTION

STOP

END TRY

'Connects with data sources related to drop-down box content

_Db = new oledbconnection ()

Try

With _db

.Connectionstring = "provider = microsoft.jet.Oledb.4.0;" & _

"Data Source = C: /NorthWind.mdb"

.Open ()

End with

Catch DBERROR AS OLEDBEXCEPTION

STOP

END TRY

_Db.close ()

'Creating a SQL statement

Strsql = "Select * from States Order by Country ASC"

Try

With new oledbdataadapter (selectcommand: = new oledbcommand (cmdtext: = strsql, _

Connection: = _ dB))

.Fill (DataSet: = _ CustomerDS, SRCTABLE: = "Country")

.Dispose ()

End with

Catch DBERROR AS OLEDBEXCEPTION

STOP

END TRY

With _CUSTOMERDS.TABLES

Objcustomercm = ctype (me.bindingcontext (.Item (name: = "Customers"), currencymanager

End with

'Generate display information

With GridtableStyle

. Mappingname = "Customers"

With .gridcolumnstyles

.Add (Column: = New DataGridTextBoxColumn (ObjcuStomercm. _

GetItemProperties.Item (name: = "Address"))))))))))

With .Item (index: = 0)

. Mappingname = "Address"

.HEADERTEXT = "Address"

.Alignment = horizontalalignment.Left

.Width = intavgcharwidth * 20

.Nulltext = String.empty

End with

_STATESDT = _CUSTOMERDS.TABLES.ITEM ("Country")

.Add (colorcomn: = new DataGridcomboBoxColumn (DataSource: = _ statesdt, _

DisplayMember: = 0, _

ValueMember: = 0)))

With .Item (INDEX: = 1)

. Mappingname = "Country". HEADERTEXT = "Country"

.Alignment = horizontalalignment.Left

.Width = intavgcharwidth * 10

.Nulltext = String.empty

End with

.Add (Column: = New DataGridTextBoxColumn (ObjcuStomercm. _

GetItemProperties.Item (name: = "City"))))))))))))

With .Item (index: = 2)

. Mappingname = "city"

.Headertext = "City"

.Alignment = horizontalalignment.Left

.Width = intavgcharwidth * 20

.Nulltext = String.empty

End with

End with

End with

With entrygrid

.CaptionText = String.empty

.Datasource = _CUSTOMERDS

.DataMember = "Customers"

.Tables.add (Table: = GridTableStyle)

End with

End Sub

Acknowledgment: This program refers to the number of online implementations, and he means to the original author.

----

Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.

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

New Post(0)