簡體   English   中英

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

[英]C# coloring the same values in row of 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;
            }
        }

如果在將數據添加到網格視圖中后,您可以遍歷行/列並對各種檢查進行編程,那么只需根據需要分配背景色?

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

如果數據來自數據源,不知道這是否有效。

想稍微改變@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;
                }            

            }

所有的建議都幫助我找到了以下解決方案,盡管 Petr 會得到 V,因為他是第一個了解我使用 cellformatting 事件的人。

    /// <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 輕松地讓它們重新開始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM