Set the DataGrid to meet the conditions of different backgrounds (WinForm).

zhaozj2021-02-16  64

Due to project needs, data lines of DataGrid are needed, and different conditions are distinguished in different backgrounds. Since there is no relevant attributes and methods in the DataGrid, you can complete this feature. I have been searching for a long time and didn't find a solution. I have to do myself, my clothes, :). After studying for a long time, I finally got it. Good things don't dare to enjoy exclusive, special post, I hope to bring some help to those needed.

{

// ...

// 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 CellcolorEventHndler (Object Sender, DataGridCellcolorEventArgs E);

Public Class DataGridColortExtBoxColumn: DataGridTextBoxColumn {Public Event CellcolorEventhandler Checkcellcolor;

Public DataGridCellColortextBoxColumn () {}

// 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-23797.html

New Post(0)