Setting the DataGrid to a different background color (WinForm)

zhaozj2021-02-16  50

{

// ...

// Use the DataGridTableStyle to display the DataGrid.

DataGridTableStyle TableStyle = New DataGridTablele (); TableStyle.MappingName = "Customers";

int numCols = _dataSet.Tables [ "customers"] Columns.Count;. DataGridCellColorTextBoxColumn columnTextColumn; for (int i = 0; i

// Establish an event for each cell. ColumnTextColumn.checkcellColor = New CellcolorEventHandler (SetColorValues);

TableStyle.GridColumnStyles.add (ColumnTextColumn); DataGrid1.tables.cle (); dataGrid1.tablestyles.add (TableStyle);

DataGrid1.datasource = _Dataset.tables ["customers"];

}

Public Void SetColorValevent (Object Sender, DataGridColorEventArgs E) {// According to the condition, the relevant line is set to set different background colors. // The following example is the row of Mexico, USA, the following example is set to red, USA Yellow. IF (datagrid1 [e.row, 8]) == "Mexico") E.BackColor = color.red; else if (traffic.tostring (datagrid1 [E.ROW, 8]) == "USA") E.BACKCOLOR = Color.Yellow;}

Public Class DataGridColorEventArgs: Eventargs {Private INT_ROW; Private Color_Backcolor

public DataGridCellColorEventArgs (int row, Color val) {_row = row; _backcolor = val;} public int Row {get {return _row;} set {_row = value;}} public Color BackColor {get {return _backcolor;} set {_backcolor = Value;}}}}}

/ / Establish a delegate. Public Delegate Void CellcolorEventHandler (Object Sender, DataGridCellcolorEventArgs E);

Public Class DataGridColortExtBoxColumn: DataGridTextBoxColumn {Public Event CellcolorEventHandler Checkcellcolor; Public DataGridColortExtBoxColumn () {}

// inherited DataGridTextBoxColumn of Pain event. Protected override void Paint (System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing. Brush foreBrush, bool alignToRight) {if (CheckCellColor = null) {// when re-painting the background color is provided in the current row DataGridCellColorEventArgs e = new DataGridCellColorEventArgs (rowNum, Color.White);! CheckCellColor (this, e); if (e . Backcolor! = Color.White) backbrush = new solidbrush (e.backcolor);}

Base.Paint (G, Bounds, Source, Rownum, Backbrush, Forebrush, Aligntoright);

protected override void Edit (System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible) {base.Edit (source, rowNum, bounds, readOnly, instantText, cellIsVisible) }}

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

New Post(0)