• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 554
  • Last Modified:

Highlight Datagrid (Winforms) row

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
IceMan713
Asked:
IceMan713
  • 4
  • 2
1 Solution
 
Bob LearnedCommented:
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
 
IceMan713Author Commented:
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
 
Bob LearnedCommented:
Aaah, yes, you want to change the BackColor of the cells based on the value from another column, right?

Bob
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
IceMan713Author Commented:
yup
0
 
IceMan713Author Commented:
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
 
IceMan713Author Commented:
how can I request for a refund?
0
 
ee_ai_constructCommented:
PAQ / Refund
ee ai construct, community support moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now