Solved

Highlight Datagrid (Winforms) row

Posted on 2006-07-06
8
540 Views
Last Modified: 2010-05-18
I'm using VS2003, C#. I have a windows form with a datagrid and I would like to highlight the whole row based on the data on the first column. Thanks in advance.
0
Comment
Question by:IceMan713
  • 4
  • 2
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17051478
Try this:

   if (this.dataGrid1[0, 0].ToString() == "Test")
      this.dataGrid1.Select(0);

 - dataGrid1[0, 0] = row #1, column #1
 - Select(0) selects row #1.

Bob
0
 
LVL 1

Author Comment

by:IceMan713
ID: 17053374
that will just select the row, I want to highlight multiple rows (not select), so that I can click on any cell and the highlight won't change.

Thanks.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17054564
Aaah, yes, you want to change the BackColor of the cells based on the value from another column, right?

Bob
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:IceMan713
ID: 17058145
yup
0
 
LVL 1

Author Comment

by:IceMan713
ID: 17058362
I got it!  Here's how I did it...

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 the cell if the first column is True
                        try
                        {
                              
                              DataTable dtPaint;

                              DataRow drPaint;

                              dtPaint =  (DataTable)this.DataGridTableStyle.DataGrid.DataSource;

                              drPaint = dtPaint.Rows[rowNum];
                              


                              //If true then highlight
                              bool blnHighlight = Convert.ToBoolean (drPaint["HighlightRow"].ToString());


                              if ( blnHighlight)

                              {

                                    backBrush = new System.Drawing.Drawing2D.LinearGradientBrush(bounds,

                                          Color.IndianRed,

                                          Color.IndianRed,

                                          System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);

                                    foreBrush = new SolidBrush(Color.White);

                              }


                        }
                        catch(Exception ex)
                        {
                              string x = ex.Message;
                        }
                        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);
                        }
                  }
0
 
LVL 1

Author Comment

by:IceMan713
ID: 17058375
how can I request for a refund?
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 17306614
PAQ / Refund
ee ai construct, community support moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now