C# 颜色不应用于datagridview行,除非它具有焦点
我正在创建一个日志表单,并在DGV中显示信息。 每个日志都以LogEntry类的形式出现 最初,我创建了数据并将其添加到DGV,如下所示:C# 颜色不应用于datagridview行,除非它具有焦点,c#,datagridview,colors,row,C#,Datagridview,Colors,Row,我正在创建一个日志表单,并在DGV中显示信息。 每个日志都以LogEntry类的形式出现 最初,我创建了数据并将其添加到DGV,如下所示: DataGridViewTextBoxColumn dateTimeColumn = new DataGridViewTextBoxColumn(); dateTimeColumn.Name = "dateTime"; dateTimeColumn.HeaderText = "Date/Time"; dataGridView_Log.Columns.Add(
DataGridViewTextBoxColumn dateTimeColumn = new DataGridViewTextBoxColumn();
dateTimeColumn.Name = "dateTime";
dateTimeColumn.HeaderText = "Date/Time";
dataGridView_Log.Columns.Add(dateTimeColumn);
创建DGV的每个列,如下所示:
DataGridViewTextBoxColumn dateTimeColumn = new DataGridViewTextBoxColumn();
dateTimeColumn.Name = "dateTime";
dateTimeColumn.HeaderText = "Date/Time";
dataGridView_Log.Columns.Add(dateTimeColumn);
添加日志条目记录:
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获得焦点,所有后续添加的行都将着色
另一个注意事项是一旦过滤,我希望颜色仍然适用于正确的行
我想要的只是闪亮的颜色:)
感谢您的时间和帮助。我不知道为什么在您选择网格中的某个内容之前,颜色不会刷新。设置颜色后是否尝试调用
.Refresh()
或.Update()
有关DataGridView
样式的有趣阅读,请参见MSDN中的以下链接:
我想我已经解决了 我添加了(创建datatable时): 然后在分配数据源之后
dataGridView_Log.Columns["Color"].Visible = false;
填充每行:
row["Color"] = logEntry.color;
添加了CellFormatting事件:
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;
}
也适用于过滤:)我也有类似的问题 我的DataGridView数据源是一个对象列表,其中许多对象可以在“Block”属性中具有相同的值。我尝试根据“块”应用颜色 我通过使用包含属性每个值的颜色的列表解决了这个问题: 属性保存颜色:
private Dictionary<Block, Color> m_BlockColors = new Dictionary<Block, Color>();
对不起,当我说“获取焦点”时,我的意思是DGV可以被“看到”。DGV位于一个单独的表单上,该表单首先实例化,然后当用户单击按钮时显示,表单关闭甚至被覆盖,它被隐藏而不是关闭。只有当表单是第一个表单时,才会应用颜色。
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];
}
}
}