locked
highlight row in c#

    Question

  • looking for an example of code to highlight a row in a data grid based on one value in one of the columns in the data grid.

    For
    Example

    Name    Credit_rating   

    Joe        Good

    Jeff       Very Good

    James    Very Bad

     

    how do I highlight the row ( James , Very  Bad) to red? I know how to higlight the cell (Very bad  to red), just cant work out how to highlight the entire row

    any help is appreciated.

     

     

    Monday, June 14, 2010 2:23 PM

Answers

  • Hi jhacharya,

       Welcome to MSDN forums! I'm glad to see your active participation and discussion in MSDN forums. Based on my understanding your issue was wanted to make highlight in datagrid If I'm not misunderstanding you I'll suggest you a good way to solve your issue, Hope it will helps! For example:

     private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
     {
     if (e.ColumnIndex == -1 || e.RowIndex == -1) return; //exclude the row head and column head
     this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Red;
     }
    
     private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
     {
     if (e.ColumnIndex == -1 || e.RowIndex == -1) return; //exclude the row head and column head
     this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.White;
     }

        If you still have any doubt and concern about this issue, please let me know. If I misunderstood you, please kindly elaborate your question.


     

    Please Mark as Answered If this is helpful Or Un-Mark as Answered if it is not helpful.

    Best Regards,
    Yan Jun
    Microsoft Online Community Support

    Tuesday, June 15, 2010 6:04 AM
  • Welcome to the MSDN Forums.

    I provided an example which does this fine. I hope helps:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
      public class Form1 : Form
      {
        public Form1()
        {
          DataGridView dgv = new DataGridView() { Dock = DockStyle.Fill };
          dgv.Columns.Add("Name", "Name");
          dgv.Columns.Add("Credit_rating", "Credit_rating");
    
          dgv.Rows.Add(new object[] { "Joe", "Good" });
          dgv.Rows.Add(new object[] { "Jeff", "Very Good" });
          dgv.Rows.Add(new object[] { "James", "Very Bad" });
    
          dgv.CellFormatting += new DataGridViewCellFormattingEventHandler(dgv_CellFormatting);
    
          this.Controls.Add(dgv);
        }
    
        void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
          DataGridView dgv = sender as DataGridView;
          if (null != dgv.Rows[e.RowIndex].Cells["Credit_rating"].Value)
          {
            switch (dgv.Rows[e.RowIndex].Cells["Credit_rating"].Value.ToString())
            {
              case "Good":
                dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Yellow;
                break;
              case "Very Good":
                dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.YellowGreen;
                break;
              case "Very Bad":
                dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Red;
                break;
            }
          }
          else
            dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = dgv.DefaultCellStyle.BackColor;
        }
      }
    }
    

    Best Wishes,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    Tuesday, June 15, 2010 6:30 AM

All replies

  • Hi jhacharya,

       Welcome to MSDN forums! I'm glad to see your active participation and discussion in MSDN forums. Based on my understanding your issue was wanted to make highlight in datagrid If I'm not misunderstanding you I'll suggest you a good way to solve your issue, Hope it will helps! For example:

     private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
     {
     if (e.ColumnIndex == -1 || e.RowIndex == -1) return; //exclude the row head and column head
     this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Red;
     }
    
     private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
     {
     if (e.ColumnIndex == -1 || e.RowIndex == -1) return; //exclude the row head and column head
     this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.White;
     }

        If you still have any doubt and concern about this issue, please let me know. If I misunderstood you, please kindly elaborate your question.


     

    Please Mark as Answered If this is helpful Or Un-Mark as Answered if it is not helpful.

    Best Regards,
    Yan Jun
    Microsoft Online Community Support

    Tuesday, June 15, 2010 6:04 AM
  • Welcome to the MSDN Forums.

    I provided an example which does this fine. I hope helps:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
      public class Form1 : Form
      {
        public Form1()
        {
          DataGridView dgv = new DataGridView() { Dock = DockStyle.Fill };
          dgv.Columns.Add("Name", "Name");
          dgv.Columns.Add("Credit_rating", "Credit_rating");
    
          dgv.Rows.Add(new object[] { "Joe", "Good" });
          dgv.Rows.Add(new object[] { "Jeff", "Very Good" });
          dgv.Rows.Add(new object[] { "James", "Very Bad" });
    
          dgv.CellFormatting += new DataGridViewCellFormattingEventHandler(dgv_CellFormatting);
    
          this.Controls.Add(dgv);
        }
    
        void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
          DataGridView dgv = sender as DataGridView;
          if (null != dgv.Rows[e.RowIndex].Cells["Credit_rating"].Value)
          {
            switch (dgv.Rows[e.RowIndex].Cells["Credit_rating"].Value.ToString())
            {
              case "Good":
                dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Yellow;
                break;
              case "Very Good":
                dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.YellowGreen;
                break;
              case "Very Bad":
                dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Red;
                break;
            }
          }
          else
            dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = dgv.DefaultCellStyle.BackColor;
        }
      }
    }
    

    Best Wishes,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    Tuesday, June 15, 2010 6:30 AM