Set the rows in the DataGrid that meet the conditions to different background colors (winform ).

Source: Internet
Author: User

Public void datagridcellcolor ()
{
//.....
Private void bindtogrid ()
{
// Obtain the data source
Coltext = new maid (1 );
Coltext. headertext = "warehouse picking ticket number ";
Coltext. width = 120;
Coltext. mappingname = "out_storagenumber ";
Coltext. textbox. DoubleClick + = new eventhandler (datagrid_doubleclick );
Tblstyle. gridcolumnstyles. Add (coltext );
Coltext. checkcellcolor + = new cellcoloreventhandler (setcellcolor );

Coltext = new maid (2 );
Coltext. headertext = "date ";
Coltext. mappingname = "thedate ";
Coltext. textbox. DoubleClick + = new eventhandler (datagrid_doubleclick );
Tblstyle. gridcolumnstyles. Add (coltext );
Coltext. checkcellcolor + = new cellcoloreventhandler (setcellcolor );

//...
Gatheringgrid. tablestyles. Add (tblstyle );
}

// Determine whether the current row meets the conditions and set the corresponding background color.
Public void setcellcolor (Object sender, datagridcellcoloreventargs E)
{
Try
{
Datetime dt = (datetime) gatheringgrid [E. Row, 2];
Int intaccountsession = 27;
If (! (Gatheringgrid [E. Row, 3] Is dbnull ))
Intaccountsession = (INT) gatheringgrid [E. Row, 3];

Timespan Ts = datetime. parse (Global. g_systemdatetime)-dT;
If (TS. days> = intaccountsession)
{
E. backcolor = color. Red;
}
Else if (TS. days> = intaccountsession-accountwarning)
{
E. backcolor = color. yellowgreen;
}
}
Catch {}
}
}
// Define event data
Public class datagridcellcoloreventargs: eventargs
{
Private int _ column;
Private int _ row;
Private color _ backcolor;

Public maid (INT row, int Col, color val)
{
_ ROW = row;
_ Column = Col;
_ Backcolor = val;
}

Public int Column
{
Get {return _ column ;}
Set {_ column = value ;}
}
Public int row
{
Get {return _ row ;}
Set {_ ROW = value ;}
}
Public color backcolor
{
Get {return _ backcolor ;}
Set {_ backcolor = value ;}
}
}

// Set the delegate.
Public Delegate void cellcoloreventhandler (Object sender, datagridcellcoloreventargs E );

// Inherit the style column.
Public class datagridcoloredtextboxcolumn: datagridtextboxcolumn
{
Private int _ Col;
Public event cellcoloreventhandler checkcellcolor;

Public datagridcoloredtextboxcolumn (INT column)
{
_ Col = column;
}

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)
{
// Set the background color of the current row.
Maid E = new maid (rownum, _ Col, color. White );
Checkcellcolor (this, e );

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)
{
// Do nothing
Base. Edit (source, rownum, bounds, readonly, instanttext, cellisvisible );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.