Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight a DataGridView row or make it glow temporarily?

Tags:

Using a C# DataGridView how can I:

  1. Highlight a row
  2. Make a row glow temporarily (go yellow for a couple of seconds)
like image 843
Craig Johnston Avatar asked Apr 13 '11 07:04

Craig Johnston


2 Answers

In order to simulate the user selecting a row, use

myDataGrid.Rows[n].IsSelected = true;

as Gabriel has suggested.

In order to temporarily color highlight a row in a DataGridView control, set the DefaultCellStyle.BackColor property to a color of your choice for the row you are interested in. Then enable a System.Windows.Forms.Timer control for the time period of your choice. When the timer's Tick event fires, disable the timer and set the row's DefaultCellStyle.BackColor back to its original color.

The short example below is for a WinForm application that has a DataGridView named GlowDataGrid, a timer named GlowTimer, and a button named GlowButton. When clicking on GlowButton, the third row of the DataGridView glows yellow temporarily for two seconds.

private void Form1_Load(object sender, EventArgs e)
    {
        // initialize datagrid with some values
        GlowDataGrid.Rows.Add(5);
        string[] names = new string[] { "Mary","James","Michael","Linda","Susan"};
        for(int i = 0; i < 5; i++)
        {
            GlowDataGrid[0, i].Value = names[i];
            GlowDataGrid[1, i].Value = i;
        }
    }

    private void GlowButton_Click(object sender, EventArgs e)
    {
        // set third row's back color to yellow
        GlowDataGrid.Rows[2].DefaultCellStyle.BackColor = Color.Yellow;
        // set glow interval to 2000 milliseconds
        GlowTimer.Interval = 2000;
        GlowTimer.Enabled = true;
    }

    private void GlowTimer_Tick(object sender, EventArgs e)
    {
        // disable timer and set the color back to white
        GlowTimer.Enabled = false;
        GlowDataGrid.Rows[2].DefaultCellStyle.BackColor = Color.White;
    }
like image 77
Jason Moore Avatar answered Oct 02 '22 13:10

Jason Moore


My Code to you

    private void Form1_Load(object sender, EventArgs e)
    {
        Timer t = new Timer();
        t.Interval = 500;
        t.Enabled = false;

        dataGridView1.CellMouseEnter += (a, b) =>
        {
            if (b.RowIndex != -1)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[b.RowIndex].Cells[0];
                dataGridView1.Rows[b.RowIndex].DefaultCellStyle.SelectionBackColor = Color.Yellow;
                dataGridView1.Rows[b.RowIndex].DefaultCellStyle.SelectionForeColor = Color.Black;
                t.Tick += (c, d) =>
                {
                    dataGridView1.Rows[b.RowIndex].DefaultCellStyle.SelectionBackColor = Color.Blue;
                    dataGridView1.Rows[b.RowIndex].DefaultCellStyle.SelectionForeColor = Color.White;
                    t.Enabled = false;
                };
                t.Enabled = true;
            }
        };
        dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        dataGridView1.Columns.Add("Col1", "Col1");
        dataGridView1.Columns.Add("Col2", "Col2");
        dataGridView1.Rows.Add("Row1", "Col1");
        dataGridView1.Rows.Add("Row1", "Col2");
        dataGridView1.Rows.Add("Row2", "Col1");
        dataGridView1.Rows.Add("Row2", "Col2");
        dataGridView1.Rows.Add("Row3", "Col1");
        dataGridView1.Rows.Add("Row3", "Col2");
        dataGridView1.Rows.Add("Row4", "Col1");
        dataGridView1.Rows.Add("Row4", "Col2");
    }
like image 30
Subhash Lama Avatar answered Oct 02 '22 12:10

Subhash Lama