【发布时间】:2010-12-22 09:19:40
【问题描述】:
我正在创建一个日志表单并在 DGV 中显示信息。 每个日志都以 LogEntry 类的形式出现。
最初我像这样创建并向 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 获得焦点,所有后续添加的行都会被着色。
另一个注释一旦被过滤,我希望颜色仍然应用于正确的行。
我想要的只是闪亮的颜色:)
感谢您的时间和帮助。
【问题讨论】:
标签: c# datagridview colors row