Results 1 to 2 of 2

Thread: windows datagrid

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    London
    Posts
    678

    windows datagrid

    hi,
    I am trying to set a different colour to a cell which is in the 2nd column in a windows datagrid depending on the value of the cell.
    (i.e. to check the date in the cell and change the colour of the cell id the date is less than today)

    This is a sample code that I have found on the internet.
    But the sample is for another purpose. It is testing for any characters higher than 'F'

    I don't seem to be able to change it for my purpose.
    Can you have a look pls?
    Thanks

    public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn

    {

    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

    // Here, we color anything that begins with a letter higher than 'F'

    try{

    object o = this.GetColumnValueAtRow(source, rowNum);

    if( o!= null)

    {

    char c = ((string)o)[0];

    if( c > 'F')

    {

    // could be as simple as

    // backBrush = new SolidBrush(Color.Pink);

    // or something fancier...

    backBrush = new LinearGradientBrush(bounds,

    Color.FromArgb(255, 200, 200),

    Color.FromArgb(128, 20, 20),

    LinearGradientMode.BackwardDiagonal);

    foreBrush = new SolidBrush(Color.White);

    }

    }

    }

    catch(Exception ex){ /* empty catch */ }

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

    }

    }

    }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: windows datagrid

    That code is casting the value as a string, then getting the first character and comparing it to 'F'. You need to cast the object as a DateTime instead and compare it to DateTime.Today. It's exactly the same principle. If the value returned by GetColumnValueAtRow is actually a DateTime then it is OK to cast. If it's actually a string though, you'd need to convert it using DateTime.Parse instead.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width