Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#为datagridview行中的相同值着色_C#_Datagridview_Colors - Fatal编程技术网

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

C#为datagridview行中的相同值着色,c#,datagridview,colors,C#,Datagridview,Colors,假设我有一个充满行的datagridview。 现在,为了使某些数据更清晰,我想给某些单元格的背景上色。 不过有一些警告,我想在其中着色的列的数量可能会有所不同。 为了让事情更清楚,我将绘制一个假的数据网格: Name Thing2 col1 col2 col3 tes test 1 1 2 t2t ers 3 3 3 der zoef 2 3 1 现在,col1-col3单元格需要着色,这取决于它们的值。第一列中的单元格将始

假设我有一个充满行的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事件

    /// <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);
                            }
                        }
                    }
                }
            }
        }
    }
首先使用
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;
            }
        }

我建议使用CellFormating事件

    /// <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);
                            }
                        }
                    }
                }
            }
        }
    }
首先使用
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;
     }
}
foreach(datagridview1.Rows中的DatGridViewEow行)
{

对于(int i=3;i如果在将数据添加到网格视图后,您可以迭代行/列并编程各种检查,然后根据需要指定背景色

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

for(int i=3;i希望稍微改变@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响应。使用此选项,您可以为行设置唯一的颜色,即使您有数千行。对于每个唯一值,它们都是与其关联的颜色。只需传递一个不超过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);
                            }
                        }
                    }
                }
            }
        }
    }
//
///将着色应用于dataGrid中的结果行
/// 
私有void ApplyColoring()
{   
if(dataGridView1.DataSource!=null)
{   
//将颜色硬映射到列
IDictionary colorDictionary=新字典();
添加(6,Color.FromArgb(194235 211));
颜色字典。添加(7,颜色。三文鱼);
颜色字典。添加(8,颜色。浅蓝色);
颜色字典。添加(9,颜色。浅黄色);
颜色字典。添加(10,颜色。浅绿色);
添加(11,Color.LightCoral);
颜色字典。添加(12,颜色。蓝色);
颜色字典。添加(13,颜色。黄色);
颜色字典。添加(14,颜色。绿色);
颜色字典。添加(15,颜色。白色);
IList checkedValues=新列表();
//首先,我们遍历所有行
foreach(DataGridViewRow gridRow在dataGridView1.Rows中)
{
IDictionary checkedVal=新字典();
//然后我们循环遍历所有的数据列
int maxCol=dnsList.Count+6;
for(int columnLoop=6;columnLoop

编辑:1月20日,带颜色的字典映射到(可能)可以着色的列。在我的应用程序中,我们永远不会得到超过10个列,但您可以通过使用MOD操作或w/e轻松地重新开始。所有建议都帮助我找到了以下解决方案,尽管Petr会得到V,因为他是第一个了解我使用cellformatting的人t

    /// <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);
                            }
                        }
                    }
                }
            }
        }
    }
//
///将着色应用于dataGrid中的结果行
/// 
私有void ApplyColoring()
{   
if(dataGridView1.DataSource!=null)
{   
//将颜色硬映射到列
IDictionary colorDictionary=新字典();
添加(6,Color.FromArgb(194235 211));
颜色字典。添加(7,颜色。三文鱼);
颜色字典。添加(8,颜色。浅蓝色);
颜色字典。添加(9,颜色。浅黄色);
颜色字典。添加(10,颜色。浅绿色);
添加(11,Color.LightCoral);
颜色字典。添加(12,颜色。蓝色);
颜色字典。添加(13,颜色。黄色);
颜色字典。添加(14,颜色。绿色);
颜色字典。添加(15,颜色。白色);
IList checkedValues=新列表();
//首先,我们遍历所有行