如何突出一个DataGridView行或使其焕发暂时? [英] How to highlight a DataGridView row or make it glow temporarily?

查看:146
本文介绍了如何突出一个DataGridView行或使其焕发暂时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#的DataGridView我怎么能:




  1. 突出显示的行

  2. 请一排辉光暂时(去黄了几秒钟)


解决方案

为了模拟用户选择行,用

  myDataGrid.Rows [N] .IsSelected = TRUE; 



加布里埃尔曾建议。



在为了暂时的颜色突出显示行DataGridView控件,在 DefaultCellStyle.BackColor 属性设置为您的选择。感兴趣的行的颜色,然后启用 System.Windows.Forms.Timer 供你选择的时间段控制。当定时器的勾选事件触发,禁止定时器并设置该行的 DefaultCellStyle.BackColor 回其原来的颜色。



下文的例子是,有一个名为GlowDataGrid DataGridView中,一个名为GlowTimer定时器,以及一个名为GlowButton按钮WinForm应用程序。当GlowButton点击,在DataGridView第三排亮起两秒钟黄色暂时的。

 私人无效Form1_Load的(对象发件人,EventArgs E)
{
//一些价值
GlowDataGrid.Rows.Add初始化数据网格(5);
的String [] =名新的字符串[] {玛丽,詹姆斯,迈克尔,琳达,苏珊};
的for(int i = 0;我小于5;我++)
{
GlowDataGrid [0,1]。价值=名称[I]
GlowDataGrid [1,I]。价值= I;
}
}

私人无效GlowButton_Click(对象发件人,EventArgs五)
{
//设置第三排座椅的背部颜色为黄色
GlowDataGrid.Rows [2] .DefaultCellStyle.BackColor = Color.Yellow;
//设置发光间隔为2000毫秒
GlowTimer.Interval = 2000;
GlowTimer.Enabled = TRUE;
}

私人无效GlowTimer_Tick(对象发件人,EventArgs五)
{
//关闭计时器并设置颜色回白色
GlowTimer.Enabled = FALSE;
GlowDataGrid.Rows [2] .DefaultCellStyle.BackColor = Color.White;
}


Using a C# DataGridView how can I:

  1. Highlight a row
  2. Make a row glow temporarily (go 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;
    }

这篇关于如何突出一个DataGridView行或使其焕发暂时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆