微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

| 使用C#DataGridView我如何: 突出显示一行 暂时使行发光(变黄几秒钟)     

解决方法

为了模拟用户选择行,请使用
myDataGrid.Rows[n].IsSelected = true;
正如加百列所建议的那样。 为了暂时为DataGridView控件中的行加亮颜色,请将
DefaultCellStyle.BackColor
属性设置为您感兴趣的行的颜色。然后在您选择的时间段内启用
System.Windows.Forms.Timer
控件。当计时器的“ 3”事件触发时,请禁用计时器,并将行的“ 1”设置回其原始颜色。 下面的简短示例适用于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] .IsSelected = true突出显示\'n \'行。     ,您可以使用
GridView
AutoFormat
属性。     ,使用方式
gridLibrary.Rows[i].DefaultCellStyle.BackColor = Color.Yellow
设置颜色,那么您将需要在 网格已排序。 然后使用计时器在延迟后更改突出显示颜色。
gridLibrary.Rows[i].DefaultCellStyle.BackColor = Color.white
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。