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

时间:2010-12-22 09:19:41

标签: c# datagridview colors row

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

最初我创建了数据并将数据添加到DGV中,如下所示:

像这样创建DGV的每个coloumn:

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获得焦点,所有随后添加的行都会被着色。

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

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

感谢您的时间和帮助。

3 个答案:

答案 0 :(得分:1)

我有类似的问题。

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

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

保存颜色的属性:

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

答案 1 :(得分:0)

我不确定为什么在你选择网格中的东西之前不会刷新颜色。您是否尝试在设置颜色后调用.Refresh().Update()

关于DataGridView样式的有趣读物是在MSDN中的这个链接:

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

答案 2 :(得分:0)

我想我已经解决了......

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

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

适用于过滤以及:)

相关问题