Solved

Highlight Datagrid (Winforms) row

Posted on 2006-07-06
8
539 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
Comment Utility
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
Comment Utility
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
Comment Utility
Aaah, yes, you want to change the BackColor of the cells based on the value from another column, right?

Bob
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:IceMan713
Comment Utility
yup
0
 
LVL 1

Author Comment

by:IceMan713
Comment Utility
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
Comment Utility
how can I request for a refund?
0
 

Accepted Solution

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

7 Experts available now in Live!

Get 1:1 Help Now