I've got a datagrid with RED, BLACK, and GREEN cells for % changes. (Thanks for the FAQ on this).
But my datagrid is sortable and the colors of the textboxes are fixed.
Can anyone suggest a solution ?
Tim
OU
Oussax
January 12, 2003 09:47 AM UTC
This may help you:
when creating your datagrid columns, let them inherit from this class and not from the DataGridTextBoxcolumn.
Now this code will go and check the data of the datagrid cell on the row selected and in my case on the column 1.
You should chose the percentage column in your case.
Then a check is going to be made on the value of the cell chosen: In my case i have 2 choices
In your case you should make the switch on the percentage value entered in the cell.
let the cases be of the type : 0 < value < 10 ..... 10 <= value < 20 ..........
Automaticaly the colors of all the row cells will change to the color selected.
I hope that this will help you. If you have any questions just mail me Moussa.Azar@ifsal.com
public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
{
TransDetail trans;
public DataGridColoredTextBoxColumn(TransDetail TransTemp)
{
trans = TransTemp;
}
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)
{
// the idea is to conditionally set the foreBrush and/or backbrush
// depending upon some crireria on the cell value
try
{
if (rowNum != trans.dtgTrans.CurrentRowIndex)
{
DataGridCell cell = new DataGridCell(rowNum,1);
// Saves the value of the cell into a string
string c = trans.dtgTrans[cell].ToString();
// Checks the value of that string with the 2 options
switch(c)
{
case "Payment":
{
backBrush = new SolidBrush(Color.Beige);
foreBrush = new SolidBrush(Color.Brown);
break;
}
case "Bill":
{
backBrush = new SolidBrush(Color.LightGray);
foreBrush = new SolidBrush(Color.Gray);
break;
}
}
}
}
catch
{
// Do nothing
}
finally
{
// make sure the base class gets called to do the drawing with
// the possibly changed brushes
base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
}
}
}
JW
Jen Wardwell
January 2, 2004 07:41 PM UTC
I am having this problem too. I was going to try your code but I couldn't figure out what transDetail variable is supposed to be. Any help?
MI
Milind
February 5, 2004 05:08 PM UTC
Has anyone figured out what the TransDetail object is all about ?
> I've got a datagrid with RED, BLACK, and GREEN cells for % changes. (Thanks for the FAQ on this).
>
> But my datagrid is sortable and the colors of the textboxes are fixed.
>
> Can anyone suggest a solution ?
>
> Tim
AD
Administrator
Syncfusion Team
February 25, 2004 05:52 PM UTC
> This may help you:
> when creating your datagrid columns, let them inherit from this class and not from the DataGridTextBoxcolumn.
> Now this code will go and check the data of the datagrid cell on the row selected and in my case on the column 1.
> You should chose the percentage column in your case.
>
> Then a check is going to be made on the value of the cell chosen: In my case i have 2 choices
> In your case you should make the switch on the percentage value entered in the cell.
> let the cases be of the type : 0 < value < 10 ..... 10 <= value < 20 ..........
>
> Automaticaly the colors of all the row cells will change to the color selected.
>
>
> I hope that this will help you. If you have any questions just mail me Moussa.Azar@ifsal.com
>
>
>
> public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
> {
> TransDetail trans;
>
> public DataGridColoredTextBoxColumn(TransDetail TransTemp)
> {
> trans = TransTemp;
> }
>
> 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)
> {
> // the idea is to conditionally set the foreBrush and/or backbrush
> // depending upon some crireria on the cell value
> try
> {
> if (rowNum != trans.dtgTrans.CurrentRowIndex)
> {
> DataGridCell cell = new DataGridCell(rowNum,1);
> // Saves the value of the cell into a string
> string c = trans.dtgTrans[cell].ToString();
>
> // Checks the value of that string with the 2 options
> switch(c)
> {
> case "Payment":
> {
> backBrush = new SolidBrush(Color.Beige);
> foreBrush = new SolidBrush(Color.Brown);
> break;
> }
> case "Bill":
> {
> backBrush = new SolidBrush(Color.LightGray);
> foreBrush = new SolidBrush(Color.Gray);
> break;
> }
> }
> }
> }
> catch
> {
> // Do nothing
> }
> finally
> {
> // make sure the base class gets called to do the drawing with
> // the possibly changed brushes
> base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
> }
> }
> }