C#在datagridview行中着色相同的值

假设我有一个填充行的datagridview。 现在为了使某些数据更加清晰,我想为某些细胞的背景着色。 但是有一些警告,我想要着色的列数可能会有所不同。 为了使事情更清楚,我将草拟一个假的数据网格:

Name Thing2 col1 col2 col3 tes test 1 1 2 t2t ers 3 3 3 der zoef 2 3 1 

现在,col1-col3细胞需要着色,具体取决于它们的值。 第一列中的单元格将始终为绿色(按照惯例),偏离它的单元格将显示为红色。 因此,第一行将col1和col2颜色为绿色,col3为红色等。 有关如何最好地解决这个问题的任何想法?

我建议使用CellFormating事件。

首先使用e.RowIndex获取与当前行关联的对象,然后根据当前列( e.ColumnIndex )和对象的属性为当前单元着色。

 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.RowIndex >= customerBindingSource.Count) return; switch (e.ColumnIndex) { case 3: Customer customer = (Customer)customerBindingSource[e.RowIndex]; if (customer.Salary > 1000) e.CellStyle.BackColor = Color.Red; break; } }
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.RowIndex >= customerBindingSource.Count) return; switch (e.ColumnIndex) { case 3: Customer customer = (Customer)customerBindingSource[e.RowIndex]; if (customer.Salary > 1000) e.CellStyle.BackColor = Color.Red; break; } } 

想稍微改变一下@Petr的反应。 使用此function,即使您有数千行,也可以为行提供唯一的颜色。 对于每一个独特的价值,它们都是与之相关的颜色。 只需要传递一个不超过32位的int值。

  private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { switch (e.ColumnIndex) { case 3: Customer customer = (Customer)customerBindingSource[e.RowIndex]; e.CellStyle.BackColor = Color.FromArgb(customer.Salary); // set unique color for each value break; } } 

如果在将数据添加到网格视图后,您可以遍历行/列并编写各种检查,然后根据需要分配背景颜色?

 foreach(DatGridViewEow row in datagridview1.Rows) { for(int i=3;i<5;i++) { DataGridViewCell cell = row.cells[i]; cell.style.backcolor = Color.Red; } } 

如果数据来自数据源,不知道是否可行。

所有这些建议都帮助我找到了以下解决方案,虽然Petr将获得V,因为他是第一个了解我使用cellformatting事件的人。

  ///  /// Applies coloring to the result rows in the dataGrid ///  private void ApplyColoring() { if (dataGridView1.DataSource != null) { // hardmap a color to a column IDictionary colorDictionary = new Dictionary(); colorDictionary.Add(6, Color.FromArgb(194, 235, 211)); colorDictionary.Add(7, Color.Salmon); colorDictionary.Add(8, Color.LightBlue); colorDictionary.Add(9, Color.LightYellow); colorDictionary.Add(10, Color.LightGreen); colorDictionary.Add(11, Color.LightCoral); colorDictionary.Add(12, Color.Blue); colorDictionary.Add(13, Color.Yellow); colorDictionary.Add(14, Color.Green); colorDictionary.Add(15, Color.White); IList checkedValues = new List(); // first we loop through all the rows foreach (DataGridViewRow gridRow in dataGridView1.Rows) { IDictionary checkedVal = new Dictionary(); // then we loop through all the data columns int maxCol = dnsList.Count + 6; for (int columnLoop = 6; columnLoop < maxCol; columnLoop++) { gridRow.Cells[columnLoop].Style.BackColor = Color.FromArgb(194, 235, 211); string current = gridRow.Cells[columnLoop].Value.ToString(); for (int checkLoop = 6; checkLoop < maxCol; checkLoop++) { string check = gridRow.Cells[checkLoop].Value.ToString(); if (!current.Equals(check)) { if (checkedVal.Keys.Contains(current)) { gridRow.Cells[columnLoop].Style.BackColor = colorDictionary[checkedVal[current]]; } else { gridRow.Cells[columnLoop].Style.BackColor = colorDictionary[columnLoop]; checkedVal.Add(current, columnLoop); } } } } } } } 

编辑:1月20日,带有颜色的字典映射到(可能的)可以着色的列。 对于我的应用程序,我们永远不会获得超过10列,但您可以通过使用MOD操作或w / e轻松地重新启动它们。