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

时间:2011-04-13 07:49:27

标签: c# datagridview

使用C#DataGridView我该怎么做:

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

6 个答案:

答案 0 :(得分:17)

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

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

答案 1 :(得分:4)

我的代码

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

答案 2 :(得分:0)

您可以通过某些DataGridView.Rows [和]突出显示'a'行.IsSelected = true;

答案 3 :(得分:0)

您可以使用GridView AutoFormat属性。

答案 4 :(得分:0)

使用

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

设置颜色,然后你需要重置颜色 网格已排序。

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

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

答案 5 :(得分:0)

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex >= 0)
        {
            int index = e.RowIndex;
            DataGridViewRow row = dataGridView1.Rows[index];
            row.Selected = true;
        }
    }