3. Code implementation
WINFORM DATAGRID structure (2) Sarrely in the article, and finally there is still to fall in practice. What is the solution? In short, it is to create a new class that can complete the DataGridTextBoxColumn class, but expands the new class of Cell color control functions, how to create, of course, inheritance:
The main role of this class In addition to the overloaded Paint mentioned above, the most important goal is how to save the user's requirements (Cell's position, Cell's background color, foreground color), passing the temples when drawing TextBox Go in the function. The implementation method is to use three collection: a reason for the recording line number (not recorded, because the drawing is saved by the column of ColumnStyle, automatically saves the list), one for recording background color, one for Record the foreground color. Please read the code yourself here. If you have any questions, you can write it in the following comments, or send it to me.
File name: cdatagridcolor.vb
Category: DataGridColoredTextBoxColumn
----------- From here to x ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Imports System.Windows.Forms
Public Class DataGridColoredTextBoxColumn
Inherits DataGridTextBoxColumn 'inherited interface
Public RowCollection As New Collection () 'For the collection of line numbers
Public backcolor () as color 'is used to store sets of background colors, indexes are consistent with the line number set (set index) 1)
Public Forecolor () AS Color 'is used to store the set, index and line number column number
Private function gettext (byval value as object) AS String
'In order to redraw the contents of the textbox, this function is increasing, and it is independent of the implementation mechanism.
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 posket
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
'Redraw on by default mode
Text = getText (Source, Rownum)) 'This sentence is to take the data mentioned above.
Backbrush = new solidbrush (TextBox.backcolor) 'background color
Forebrush = new solidbrush (TextBox.forecolor) 'Prospective "Prevent users from do not define a set size
Redim Preserve Backcolor (RowCollection.count)
Redim preserve forecolor (rowcollection.count)
DIM I as integer = 1 'Collection index starts from 1
Do While (i <= rowcolction.count)
If rownum = val (RowCollection.Item (i)) The number of rows in the collection is
IF not Backcolor (i - 1) .Isempty Then 'is not defined by default color
Backbrush = New Solidbrush (Backcolor (i - 1))
END IF
IF not Forecolor (i - 1) .Isempty Then 'is not defined by default color
Forebrush = New Solidbrush (Forecolor (i - 1))
END IF
END IF
i = 1
Loop
MyBase.PaintText (G, Bounds, Text, Backbrush, Forebrush, Aligntoright)
End Sub
END CLASS
------------------- X-class end x ------------------------ ---------
Below is the code of the "Callo" button (ID: button2) in my example. There are two types of discoloration: the specified position discoloration, dynamically discolored according to the data source condition determination results:
------------------ X code X ------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------
Private sub button2_click (byval sender as system.object, byval e as system.eventargs) _
Handles Button2.click
DIM DT AS DATATABLE
DIM TS AS New DataGridtableStyle ()
DIM AcolumnTextColumn As DataGridColoredTextBoxColumn 'The above custom class
DT = DataSet11.Tables ("Table") 'DataSet11 Please define it, or look at my previous article
DataGrid1.dataSource = DT 'Data Source Binding
Ts.mappingname = dt.tablename
DIM Numcols as INTEGER
Numcols = dataset11.tables ("Table"). Columns.count 'Statistics Columns
DIM I, J AS Integer
i = 0
J = 0
Do While (i acolumnTextColumn = New DataGridColoredTextBoxColumn () IF i = 0 THEN AcolumnTextColumn.rowCollection.add (0) 'Specify the number of lines AcolumnTextColumn.RowCollection.Add (2) Redim acolumntextColumn.ForeColor (acolumnTextColumn.RowCollection.count) Redim acolumntextColumn.backcolor (acolumntextColumn.RowCollection.count) 'does not assign the default color, the background is white, the foreground is black AcolumnTextColumn.ForeColor (0) = color.red AcolumnTextColumn.ForeColor (1) = color.blue AcolumnTextColumn.backcolor (0) = color.pink AcolumnTextColumn.backcolor (1) = color.aqua END IF IF i = 1 THEN For j = 0 to dataset11.tables ("Table"). Rows.count - 1 If DataSet11.Tables ("Table"). Rows (j)> 2 Then 'Which Cell will be greater than 2 in the judge AcolumnTextColumn.RowCollection.Add (j) Redim columnTextColumn.ForeColor (acolumnTextColumn.RowCollection.count) Redim acolumntextColumn.backcolor (acolumntextColumn.rowcollection.count) AcolumnTextColumn.ForeColor (0) = color.white AcolumnTextColumn.backcolor (0) = color.black END IF NEXT END IF 'To change the list name, please refer the Headertext value of the sentence AcolumnTextColumn.Headertext = DataSet11.Tables ("Table"). ColumnS (i) .columnname AcolumnTextColumn.mappingName = DataSet11.tables ("Table"). Column (i) .COLUMNNAME Ts.GridColumnStyles.Add (AcolumnTextColumn) i = (i 1) Loop DataGrid1.tables.Add (TS) End Sub Here, it will be understood here, and it is necessary to pay attention to the same type, in this example, the type of second column is a digital type, so it is directly compared with the digital 2. Bless you, see you next time! ---- 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. QQ: 18349592 E-mail: Henry7685@hotmail.com Please visit my column: http://www.9cbs.net/develop/author/netauthor/latitude/