Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何高亮显示DataGridView行或使其暂时发光?_C#_Datagridview - Fatal编程技术网

C# 如何高亮显示DataGridView行或使其暂时发光?

C# 如何高亮显示DataGridView行或使其暂时发光?,c#,datagridview,C#,Datagridview,使用C#DataGridView,我如何能够: 突出显示一行 使行暂时发光(变黄几秒钟) 您可以通过someDataGridView高亮显示“n”行。行[n]。IsSelected=true 您可以使用GridViewAutoFormat属性。要模拟用户选择行,请使用 myDataGrid.Rows[n].IsSelected = true; 正如加布里埃尔所建议的 要临时为DataGridView控件中的行加亮显示颜色,请将DefaultCellStyle.BackColor属性设置为感兴趣

使用C#DataGridView,我如何能够:

  • 突出显示一行
  • 使行暂时发光(变黄几秒钟)

  • 您可以通过someDataGridView高亮显示“n”行。行[n]。IsSelected=true

    您可以使用
    GridView
    AutoFormat
    属性。

    要模拟用户选择行,请使用

    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\u加载(对象发送方,事件参数e)
    {
    //使用一些值初始化datagrid
    GlowDataGrid.Rows.Add(5);
    字符串[]名称=新字符串[]{“玛丽”、“詹姆斯”、“迈克尔”、“琳达”、“苏珊”};
    对于(int i=0;i<5;i++)
    {
    GlowDataGrid[0,i].Value=names[i];
    GlowDataGrid[1,i].Value=i;
    }
    }
    私有无效发光按钮\u单击(对象发送者,事件参数e)
    {
    //将第三行的背面颜色设置为黄色
    GlowDataGrid.Rows[2].DefaultCellStyle.BackColor=Color.Yellow;
    //将发光间隔设置为2000毫秒
    时间间隔=2000;
    GlowTimer.Enabled=true;
    }
    私有void GlowTimer_Tick(对象发送方,事件参数e)
    {
    //禁用计时器并将颜色设置回白色
    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");
        }
    

    gridLibrary.Rows[i].DefaultCellStyle.BackColor = Color.Yellow
    
    要设置颜色,则需要在设置后重置颜色 网格已排序

    然后使用定时器更改延迟后的高光颜色

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

    当您选择行或默认情况下,暂时处于选中状态。您可以通过网格的selectedrowstyle执行1,但通过辉光行执行什么操作temporarily@Craig,你现在忘了说。在我之前的评论之后,我看到你接受了9个答案。你得到答案了吗。如果是,请分享,它可能会帮助其他人被选中而不是被选中(i)应该是[i]C#问题
    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                int index = e.RowIndex;
                DataGridViewRow row = dataGridView1.Rows[index];
                row.Selected = true;
            }
        }