0

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.

3 Answers 3

1

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

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

1
  • Sorry when I say get focus i meant that the DGV can be 'seen'. The DGV is on a separate form which is first instanced then when ever a user clicks a button it is shown, the forms closing even is overwritten and it is hidden instead of closed. Only once the form is first form.show() will the colours be applied.
    – Metalstorm
    Dec 22, 2010 at 10:56
0

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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.