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 );
}
}