如何突出显示DataGridView行或使其暂时发光?

使用C#DataGridView我该怎么做:

  1. 突出显示一行
  2. 暂时发光(变黄几秒钟)

为了模拟用户选择一行,请使用

myDataGrid.Rows[n].IsSelected = true; 

正如加布里埃尔所说的那样。

若要临时颜色突出显示DataGridView控件中的行,请将DefaultCellStyle.BackColor属性设置为您感兴趣的行所选的颜色。然后在您的时间段内启用System.Windows.Forms.Timer控件。选择。 当计时器的Tick事件触发时,禁用计时器并将行的DefaultCellStyle.BackColor设置回其原始颜色。

下面的简短示例适用于WinForm应用程序,该应用程序具有名为GlowDataGrid的DataGridView,名为GlowTimer的计时器和名为GlowButton的按钮。 单击GlowButton时,DataGridView的第三行暂时呈现黄色两秒钟。

 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; } 

我的代码给你

  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"); } 

您可以通过someDataGridView.Rows [n]突出显示’n’行.IsSelected = true;

您可以使用GridView AutoFormat属性。

使用像

 gridLibrary.Rows[i].DefaultCellStyle.BackColor = Color.Yellow 

若要设置颜色,则需要在网格排序后重置颜色。

然后使用计时器在延迟后更改高亮颜色。

 gridLibrary.Rows[i].DefaultCellStyle.BackColor = Color.white