Customized to DataGrid color in VB.NET.

zhaozj2021-02-16  66

Recently, the project uses a color of the colors or columns similar to the Web Control DataGrid, but the application is in the WIN form, it is unable to use the functionality of the script registration similar to JavaScript to dynamically complete, very anxious, look Some of the 9CBS about the data about DataGrid under WinForm. Seeing such an introduction to the DG structure, the topic is "Henry Instruction: Winform DataGrid Structure Analysis", the author is a Latitude, which introduces Win DG. The definition of the color is mainly for each Cell.

What we need is the data marked a row, highlighting the color, so I have made some changes, and now some code is posted out for the reference:

1. The basic class is from Han Rui:

URL: http://www.9cbs.net/develop/read_article.asp?id=15686

Public Class DataGridColoredTextBoxColumn

Inherits DataGridTextBoxColumn

Public RowCollection As New Collection ()

Public Backcolor () As Color

Public Forecolor () AS Color

Private function gettext (byval value as object) AS String

If Typeof (Value) is system.dbnull the

Return NullText

Elseif value is nothing thein

Return ""

Else

Return Value.toString

END IF

END FUNCTION

Protected Overloads Overrides Sub Paint (Byval g as system.drawing.graphics, byval bounds as system.drawing.Rectangle, _

Byval source as system.windows.Forms.currencymanager, _

Byval rownum as integer, _

BYVAL backbrush as system.drawing.brush, _

Byval forebrush as system.drawing.brush, _

Byval aligntoright as boolean)

Dim text as string

TEXT = GetText (getColumnValueatrow (Source, Rownum))

Backbrush = new solidbrush (TextBox.backcolor)

Forebrush = new solidbrush (TextBox.ForeColor)

Redim Preserve Backcolor (RowCollection.count)

Redim preserve forecolor (rowcollection.count)

DIM i as integer = 1

Do While (i <= rowcolction.count)

If rownum = val (RowCollection.Item (i)) THEN

IF not Backcolor (i - 1) .Isempty Then

Backbrush = New Solidbrush (Backcolor (i - 1))

END IF

IF not Forecolor (i - 1) .Isempty Then

Forebrush = New Solidbrush (Forecolor (i - 1)) Endiff

END IF

i = 1

Loop

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

End Sub

END CLASS

2. About the colors defined by the line:

Imports System.Windows.Forms

Namespace TRUCK_WEB

Public Class DrawDGCLASS

Public Class RedrawDataDridControls: Inherits DataGridColoredTextBoxColumn

Public Sub Drawcorol (Byref DG As DataGrid, Optional Byval CurrentrowIndex as INTEGER = 0)

'Set the color of the selected row, the default is the first line.

DIM DT AS DATATABLE

DIM TS AS New DataGridtableStyle ()

Ts.Allowsorting = FALSE

DIM AcolumnTextColumn as DataGridColoredTextBoxColumn

Dt = ctype (Dg.DataSource, DataTable)

Ts.mappingname = ctype (DG.DataSource, DataTable) .tablename

Dg.TablesTyles.clear ()

DIM Numcols as INTEGER

Numcols = dt.columns.count

DIM I, J AS Integer

i = 0

J = 0

Do While (i

acolumnTextColumn = New DataGridColoredTextBoxColumn ()

Dim RowIndex as INTEGER = 0

For rowindex = 0 to dt.rows.count - 1

Dim Strsel As String

Dim MyForecorol, MybackCorol As Color

AcolumnTextColumn.RowCollection.Add (Rowindex)

If RowIndex = Currentrowindex Then

MyForecorol = color.White

Mybackcorol = color.darkslateBlue

Else

MyForecorol = color.darkslateBlue

Mybackcorol = color.White

END IF

Redim Preserve AcolumnTextColumn.ForeColor (acolumntextColumn.RowCollection.count)

Redim preserve acolumntextColumn.backcolor (acolumntextcolumn.rowcollection.count)

AcolumnTextColumn.ForeColor (RowIndex) = MyForecorol

AcolumnTextColumn.backcolor (RowIndex) = MybackCorol

NEXT

'To change the list name, please refer the Headertext value of the sentence

AcolumnTextColumn.Headertext = DT.COLUMNS (i) .Columnname

AcolumnTextColumn.mappingname = dt.columns (i) .ColumnNamets.GridColumnStyles.Add (acolumnTextColumn)

i = (i 1)

Loop

Dg.TablesTyles.Add (TS)

End Sub

END CLASS

END CLASS

End Namespace

The above is the color of the selected single line of color is the anti-color, you can also borrow your questions! For example, set color, and so on.

Han Rui, the author of "Henry Intention: Winform DataGrid Structure", the author of Han Rui!

I will try to improve this DrawDG class in the future, and it is convenient for everyone!

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

New Post(0)