代码之家  ›  专栏  ›  技术社区  ›  Oxymoron

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

  •  2
  • Oxymoron  · 技术社区  · 15 年前

    假设我有一个充满行的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颜色为红色等等。 我有什么好办法解决这个问题吗?

    4 回复  |  直到 15 年前
        1
  •  2
  •   Petr Havlicek    15 年前

    首先使用获取与当前行关联的对象 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;
                }
            }
    
    
        2
  •  1
  •   Asad    15 年前

    想稍微改变一下@Petr的反应。使用此选项,即使有上千行,也可以为行设置独特的颜色。对于每一种独特的价值,它们都是与之相关联的颜色。只需要传递一个不超过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;
                    }            
    
                }
    
        3
  •  1
  •   David Hall    15 年前

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

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

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

        4
  •  0
  •   Oxymoron    15 年前

        /// <summary>
        /// Applies coloring to the result rows in the dataGrid
        /// </summary>
        private void ApplyColoring()
        {   
            if (dataGridView1.DataSource != null)
            {   
                // hardmap a color to a column
                IDictionary<Int32, Color> colorDictionary = new Dictionary<Int32, Color>();
                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<String> checkedValues = new List<String>();
    
                // first we loop through all the rows
                foreach (DataGridViewRow gridRow in dataGridView1.Rows)
                {
                    IDictionary<String, Int32> checkedVal = new Dictionary<String, Int32>();
    
                    // 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轻松地使它们重新开始。