颜色不会应用于 datagridview 行,除非它具有焦点

发布于 2024-10-08 21:30:34 字数 1618 浏览 0 评论 0原文

我正在创建一个日志表单并在 DGV 中显示信息。 每个日志都以 LogEntry 类的形式出现。

最初,我像这样创建 DGV 并将数据添加到 DGV:

像这样创建 DGV 的每一列:

DataGridViewTextBoxColumn dateTimeColumn = new DataGridViewTextBoxColumn();
dateTimeColumn.Name = "dateTime";
dateTimeColumn.HeaderText = "Date/Time";
dataGridView_Log.Columns.Add(dateTimeColumn);

添加 logEntry 记录:

dataGridView_Log.Rows.Add(logEntry.dateTime, logEntry.service, logEntry.command, logEntry.message);
dataGridView_Log.Rows[dataGridView_Log.Rows.Count - 1].DefaultCellStyle.ForeColor = logEntry.color;

这一切都很好,每行都涂上了正确的颜色,但作为一个日志,它包含很多条目,所以我希望能够随意过滤它。

环顾四周后,我似乎无法使用我所拥有的方法进行过滤(也许我只是还没有找到正确的示例?)所以我恢复到这个方法:

添加列:

    //Create a new DataTable
    dt = new DataTable("Logs");

    //Add columns to datatable
    dt.Columns.Add("dateTime", typeof(string));
    dt.Columns.Add("Service", typeof(string));
    dt.Columns.Add("Command", typeof(string));
    dt.Columns.Add("Message", typeof(string));

    //Set the dataGridView's dataSoure to the filled dataTable
    dataGridView_Log.DataSource = dt;

添加行:

  row = dt.NewRow();

  row["dateTime"] = logEntry.dateTime;
  row["Service"] = logEntry.service;
  row["Command"] = logEntry.command;
  row["Message"] = logEntry.message;

  dt.Rows.Add(row);

   dataGridView_Log.Rows[dataGridView_Log.Rows.Count - 1].DefaultCellStyle.ForeColor = logEntry.color;

但是,这就是问题,如果 DGV没有焦点,颜色不会应用于任何行,它们只是黑色文本。但一旦 DGV 获得焦点,所有后续添加的行就会被着色。

另一个注释是,一旦过滤,我希望颜色仍然应用于正确的行。

我想要的只是闪亮的颜色:)

感谢您的时间和帮助。

I'm creating a log form and displaying the infomation in a DGV.
Each log comes in the form of a LogEntry class.

originally I created and added data to the DGV like so:

Creating each coloumn of the DGV like so:

DataGridViewTextBoxColumn dateTimeColumn = new DataGridViewTextBoxColumn();
dateTimeColumn.Name = "dateTime";
dateTimeColumn.HeaderText = "Date/Time";
dataGridView_Log.Columns.Add(dateTimeColumn);

Adding a logEntry record:

dataGridView_Log.Rows.Add(logEntry.dateTime, logEntry.service, logEntry.command, logEntry.message);
dataGridView_Log.Rows[dataGridView_Log.Rows.Count - 1].DefaultCellStyle.ForeColor = logEntry.color;

This all works fine and each row is coloured the right colour, but being a log it holds a lot of entries so i would like to be able to filter it at will.

After looking around it seems i cant filter using the method I had (might be able to I just havent found the right example?) so i reverted back to this method:

Adding columns:

    //Create a new DataTable
    dt = new DataTable("Logs");

    //Add columns to datatable
    dt.Columns.Add("dateTime", typeof(string));
    dt.Columns.Add("Service", typeof(string));
    dt.Columns.Add("Command", typeof(string));
    dt.Columns.Add("Message", typeof(string));

    //Set the dataGridView's dataSoure to the filled dataTable
    dataGridView_Log.DataSource = dt;

Adding rows:

  row = dt.NewRow();

  row["dateTime"] = logEntry.dateTime;
  row["Service"] = logEntry.service;
  row["Command"] = logEntry.command;
  row["Message"] = logEntry.message;

  dt.Rows.Add(row);

   dataGridView_Log.Rows[dataGridView_Log.Rows.Count - 1].DefaultCellStyle.ForeColor = logEntry.color;

However here is the problem, if the DGV doesnt have focus the colour isnt applied to any of the rows and they are just black text. But as soon as the DGV gets focus all following added rows are then coloured.

Another note is once filtered i want the colour to still be applied to the correct rows.

All I want is shiny colours :)

Thank you for your time and help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞清仓 2024-10-15 21:30:34

我有一个类似的问题。

我的 DataGridView 数据源是一个对象列表,其中许多对象可以在“块”属性中具有相同的值。我尝试根据“块”应用颜色。

我通过使用包含属性每个值的颜色的列表解决了问题:

保存颜色的属性:

private Dictionary<Block, Color> m_BlockColors = new Dictionary<Block, Color>();

代码:

DataGridViewCell blockCell = null;
if (dataGridView.Columns.Contains(columnNameBlock))
{
    blockCell = dataGridView[columnNameBlock, e.RowIndex];
}
if (blockCell != null)
{
    if (blockCell.Value == null)
    {
        e.CellStyle.BackColor = Color.Red;
    }
    else
    {
        Block blockOfCurrentRow = (Block)blockCell.Value;
        Block blockOfRowBefore = null;

        // Wont hit at first Row!
        if (e.RowIndex > 0)
        {
            blockOfRowBefore = (Block)dataGridView[columnNameBlock, e.RowIndex - 1].Value;
        }

        if (blockOfRowBefore != null)
        {
            //Trace.WriteLine("------------------------------------------------------");
            //Trace.WriteLine(String.Format("RowIndex: {0}", e.RowIndex));
            //Trace.WriteLine(String.Format("ColumnIndex: {0}", e.ColumnIndex));
            //Trace.WriteLine(String.Format("Current Block: {0}", blockOfCurrentRow.BlockNummer));
            //Trace.WriteLine(String.Format("Prev Block: {0}", blockOfRowBefore.BlockNummer));

            if (blockOfCurrentRow == blockOfRowBefore)
            {
                e.CellStyle.BackColor = m_BlockColors[blockOfCurrentRow];
            }
            else
            {
                // Previous Row was gray:
                if (m_BlockColors[blockOfRowBefore] == Color.LightGray)
                {
                    if (!m_BlockColors.ContainsKey(blockOfCurrentRow))
                    {
                        m_BlockColors.Add(blockOfCurrentRow, Color.White);
                    }
                }
                // Previous Row was white:
                else
                {
                    if (!m_BlockColors.ContainsKey(blockOfCurrentRow))
                    {
                        m_BlockColors.Add(blockOfCurrentRow, Color.LightGray);
                    }
                }
                e.CellStyle.BackColor = m_BlockColors[blockOfCurrentRow];
            }
        }
        else
        {
            // first Row
            if (!m_BlockColors.ContainsKey(blockOfCurrentRow))
            {
                m_BlockColors.Add(blockOfCurrentRow, Color.White);
            }
            e.CellStyle.BackColor = m_BlockColors[blockOfCurrentRow];
        }
    }
}

I had a similar Problem.

My DataGridView DataSource is a List of Objects, where many Objects can have the same Value in the "Block" Property. I tried to apply colors based on the "Block".

I solved the Problem by using a List containing the Color for each Value of the Property:

Property to Save Colors:

private Dictionary<Block, Color> m_BlockColors = new Dictionary<Block, Color>();

Code:

DataGridViewCell blockCell = null;
if (dataGridView.Columns.Contains(columnNameBlock))
{
    blockCell = dataGridView[columnNameBlock, e.RowIndex];
}
if (blockCell != null)
{
    if (blockCell.Value == null)
    {
        e.CellStyle.BackColor = Color.Red;
    }
    else
    {
        Block blockOfCurrentRow = (Block)blockCell.Value;
        Block blockOfRowBefore = null;

        // Wont hit at first Row!
        if (e.RowIndex > 0)
        {
            blockOfRowBefore = (Block)dataGridView[columnNameBlock, e.RowIndex - 1].Value;
        }

        if (blockOfRowBefore != null)
        {
            //Trace.WriteLine("------------------------------------------------------");
            //Trace.WriteLine(String.Format("RowIndex: {0}", e.RowIndex));
            //Trace.WriteLine(String.Format("ColumnIndex: {0}", e.ColumnIndex));
            //Trace.WriteLine(String.Format("Current Block: {0}", blockOfCurrentRow.BlockNummer));
            //Trace.WriteLine(String.Format("Prev Block: {0}", blockOfRowBefore.BlockNummer));

            if (blockOfCurrentRow == blockOfRowBefore)
            {
                e.CellStyle.BackColor = m_BlockColors[blockOfCurrentRow];
            }
            else
            {
                // Previous Row was gray:
                if (m_BlockColors[blockOfRowBefore] == Color.LightGray)
                {
                    if (!m_BlockColors.ContainsKey(blockOfCurrentRow))
                    {
                        m_BlockColors.Add(blockOfCurrentRow, Color.White);
                    }
                }
                // Previous Row was white:
                else
                {
                    if (!m_BlockColors.ContainsKey(blockOfCurrentRow))
                    {
                        m_BlockColors.Add(blockOfCurrentRow, Color.LightGray);
                    }
                }
                e.CellStyle.BackColor = m_BlockColors[blockOfCurrentRow];
            }
        }
        else
        {
            // first Row
            if (!m_BlockColors.ContainsKey(blockOfCurrentRow))
            {
                m_BlockColors.Add(blockOfCurrentRow, Color.White);
            }
            e.CellStyle.BackColor = m_BlockColors[blockOfCurrentRow];
        }
    }
}
昔梦 2024-10-15 21:30:34

我不确定为什么颜色不会刷新,直到您在网格中选择某些内容。您是否尝试在设置颜色后调用 .Refresh().Update()

有关 DataGridView 样式的有趣读物位于 MSDN 中的以下链接:

http://msdn.microsoft.com/en-us/library/1yef90x0.aspx

I'm not sure why the colours don't refresh until you select something in the grid. Have you tried calling .Refresh() or .Update() after setting the colours?

An interesting read about DataGridView styles is at this link in MSDN:

http://msdn.microsoft.com/en-us/library/1yef90x0.aspx

瑾夏年华 2024-10-15 21:30:34

我想我解决了它......

我添加(创建数据表时):

dt.Columns.Add("Color", typeof(Color));

数据源之后

dataGridView_Log.Columns["Color"].Visible = false;

然后在分配填充每一行的

row["Color"] = logEntry.color;

:添加单元格格式化事件:

    private void dataGridView_Log_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        dataGridView_Log.Rows[e.RowIndex].DefaultCellStyle.ForeColor = (Color)dataGridView_Log.Rows[e.RowIndex].Cells["Color"].Value;
    }

也可以与过滤一起使用:)

I think ive solved it...

i added (when creating the datatable):

dt.Columns.Add("Color", typeof(Color));

then after assigning the datasource

dataGridView_Log.Columns["Color"].Visible = false;

filling each row:

row["Color"] = logEntry.color;

added cellformating event:

    private void dataGridView_Log_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        dataGridView_Log.Rows[e.RowIndex].DefaultCellStyle.ForeColor = (Color)dataGridView_Log.Rows[e.RowIndex].Cells["Color"].Value;
    }

Works with filtering aswell :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文