How to highlight a DataGridView row or make it glow temporarily?
Using a C# DataGridView how can I:
- Highlight a row
- Make a row glow temporarily (go 开发者_StackOverflow中文版yellow for a couple of seconds)
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;
}
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");
}
Use like
gridLibrary.Rows[i].DefaultCellStyle.BackColor = Color.Yellow
to set the color, then you will need to reset the colors after the grid is sorted.
Then using the timer to change the highlight color after delay.
gridLibrary.Rows[i].DefaultCellStyle.BackColor = Color.white
You can highlight 'n' row by someDataGridView.Rows[n].IsSelected = true;
You can use the GridView
AutoFormat
property.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
int index = e.RowIndex;
DataGridViewRow row = dataGridView1.Rows[index];
row.Selected = true;
}
}
In case anyone is using AlternatingRowsDefaultCellStyle.BackColor and RowsDefaultCellStyle.BackColor properties on dataGridView, setting the BackColor on a Row will override these style values. Therefore use Color.Empty instead of Color.White to reset the BackColor to its default value, thus restoring the AlternatingRowsDefaultCellStyle and RowsDefaultCellStyle values.
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Empty;
精彩评论