C# DataGridView单元格颜色更改

C# DataGridView单元格颜色更改,c#,.net,winforms,C#,.net,Winforms,我有一个测试程序,它构建一个datatable(26行6列)作为DataGridView的数据源。 可以通过选择单元格并输入3个NumUpDown控件之一的值来编辑datagrid。 每个控件都有特定的颜色。为了保留信息,我将表保存在用户设置中。 我将每个单元格的颜色信息保存在额外的表格单元格中。 问题:当我显示datagrid时,它从表中读取OK值,但根据额外单元格信息通过编程添加的单元格颜色不显示。 (我可以在诊断中看到单元格的颜色正确,但它没有显示在数据网格中。 看起来DataGrid有一

我有一个测试程序,它构建一个datatable(26行6列)作为DataGridView的数据源。
可以通过选择单元格并输入3个NumUpDown控件之一的值来编辑datagrid。
每个控件都有特定的颜色。为了保留信息,我将表保存在用户设置中。
我将每个单元格的颜色信息保存在额外的表格单元格中。
问题:当我显示datagrid时,它从表中读取OK值,但根据额外单元格信息通过编程添加的单元格颜色不显示。
(我可以在诊断中看到单元格的颜色正确,但它没有显示在数据网格中。
看起来DataGrid有一些颜色覆盖(?。
附件是完整的工作代码。
查看代码中的更多信息。
任何人都可以运行此程序并找到为什么颜色不显示在数据网格中? 谢谢

使用系统;
使用系统数据;
使用系统图;
使用System.Web.UI.WebControl;
使用System.Windows.Forms;
//这是一个测试程序,用于执行以下操作:
//1.构建一个26行6列的空数据表。
//2.该表被绑定(作为数据源)到DataGridView。
//3.DataGrid列0-3可以由3个NumericUpDown控件中的一个填充小数。
//3.对于cols 0-2中的每个单元格,cols 3-5中有一个单元格保存使用NumUpDn的信息。
//4.每个NumUpDn都有特定的颜色。相应的单元格具有相同的背景色。
//5.当程序启动时,它根据设置构建表格,并在datagrid中显示表格。
//6.cols 0-2中的单元格应具有cols 3-5中指示的颜色,但它们没有。
//要运行,请创建一个带有datagrid“fareGrid”和3个带有背景色的NumUpDn控件的WinForm。
命名空间DataGridViewTest
{
公共部分类Form1:Form
{
DataTable fareTable=新DataTable();
整数行索引;
int-colIndex;
公共表格1()
{
初始化组件();
建筑价格表(“价格表”);
//加载表(如果存在)
if(Properties.Settings.Default.fareTable!=null)
fareTable=Properties.Settings.Default.fareTable;
paintFareTableCells(fareTable);
}
//建立26R X 6C的初始数据表
//列3-5用于保存cols 0-2的颜色信息
public void buildFareTable(字符串fareTableName)
{
fareTable.TableName=fareTableName;
票价表.Columns.Add(“工作日”,类型(十进制));
票价表。列。添加(“半天”,类型(十进制));
票价表。列。添加(“周末”,类型(十进制));
//添加3个索引列,用于保存表格单元格组
fareTable.Columns.Add(“IWeekday”,typeof(String));
fareTable.Columns.Add(“IHalfDay”,typeof(String));
fareTable.Columns.Add(“Iweekend”,typeof(String));
对于(rowIndex=0;rowIndex<26;rowIndex++)
{
fareTable.Rows.Add();
}
}
公共表格FareTableCells(DataTable fareTable)
{
fareDataGrid.DataSource=fareTable;
对于(rowIndex=0;rowIndex<26;rowIndex++)
{
对于(colIndex=0;colIndex<3;colIndex++)
{
开关(fareTable.Rows[rowIndex][colIndex+3].ToString())//检查颜色索引列3-5
{
案例“低”:fareDataGrid[colIndex,rowIndex]。Style.BackColor=Color.Green;
打破
案例“med”:fareDataGrid[colIndex,rowIndex]。Style.BackColor=Color.Yellow;
打破
案例“高”:fareDataGrid[colIndex,rowIndex]。Style.BackColor=Color.Red;
打破
违约:
打破
}
//诊断:检查电池的颜色。此时,颜色正常!
Color Color=fareDataGrid[colIndex,ROWDINDEX].Style.BackColor;
}
}
}
//通过3个NumUpDn控件更新单元格。
private void numericUpDownFareHigh_值已更改(对象发送方,事件参数e)
{
编辑单元(发送方);
}
private void numericUpDownFareMed_值已更改(对象发送方,事件参数e)
{
编辑单元(发送方);
}
private void numericUpDownFareLow\u值已更改(对象发送方,事件参数e)
{
编辑单元(发送方);
}
公用表格单元格(对象发送方)
{
foreach(fareDataGrid.SelectedCells中的DataGridViewCell单元格)
{
if(cell.ColumnIndex<3)
{
Value=((NumericUpDown)sender.Value;
cell.Style.BackColor=((NumericUpDown)sender.BackColor;
fareDataGrid[cell.ColumnIndex+3,cell.RowIndex].Value=((NumericUpDown)sender.Tag;//使用cols 3-5保存成本水平信息。
}
}
}
//使用设置保存数据
私有作废Form1\u FormClosing(对象发送方,FormClosingEventArgs e)
{
Properties.Settings.Default.fareTable=this.fareTable;
Properties.Settings.Default.Save();
}
}
}

在您的
paintFareTableCells
中,case语句很可能会进入
默认值,因此不会设置任何值。请使用断点确认此情况,并相应修改列索引


我希望这有帮助。

颜色更改代码必须在事件处理程序中;特别是
DataBindingComplete
事件处理程序。我重新编写了代码,如下所示:

public void paintFareTableCells(DataTable fareTable)
{
    fareDataGrid.DataSource = fareTable;
}

void fareDataGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    for (rowIndex = 0; rowIndex < 26; rowIndex++)
    {
        for (colIndex = 0; colIndex < 3; colIndex++)
        {
            switch (fareTable.Rows[rowIndex][colIndex + 3].ToString())   //Check the color index columns 3-5
            {
                case "low":
                    fareDataGrid[colIndex, rowIndex].Style.BackColor = Color.Green;
                    break;
                case "med":
                    fareDataGrid[colIndex, rowIndex].Style.BackColor = Color.Yellow;
                    break;
                case "high":
                    fareDataGrid[colIndex, rowIndex].Style.BackColor = Color.Red;
                    break;
                default:
                    break;
            }

            // Diagnostics: Check cell's color. At this point, Colors are OK!  
            Color color = fareDataGrid[colIndex, rowIndex].Style.BackColor;
        }
    }
}

不幸的是,我不知道为什么你的细胞没有被画成他们应该的样子,但我最好的猜测是你试图在他们处理他们的绘画事件之前改变他们的属性。 正如Joe Sisk所建议的那样,我成功地用event改变了单元格的颜色,这对我来说很好。我希望它能帮助你。区别在于我用了CellPaint eve
public void paintFareTableCells(DataTable fareTable)
{
    fareDataGrid.DataSource = fareTable;
}

void fareDataGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    for (rowIndex = 0; rowIndex < 26; rowIndex++)
    {
        for (colIndex = 0; colIndex < 3; colIndex++)
        {
            switch (fareTable.Rows[rowIndex][colIndex + 3].ToString())   //Check the color index columns 3-5
            {
                case "low":
                    fareDataGrid[colIndex, rowIndex].Style.BackColor = Color.Green;
                    break;
                case "med":
                    fareDataGrid[colIndex, rowIndex].Style.BackColor = Color.Yellow;
                    break;
                case "high":
                    fareDataGrid[colIndex, rowIndex].Style.BackColor = Color.Red;
                    break;
                default:
                    break;
            }

            // Diagnostics: Check cell's color. At this point, Colors are OK!  
            Color color = fareDataGrid[colIndex, rowIndex].Style.BackColor;
        }
    }
}
fareDataGrid.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(fareDataGrid_DataBindingComplete);
    private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        if (e.Value != null) // if condition is met, this cell will be painted
        {
            e.CellStyle.BackColor = Color.Black; // i used black color, feel free to replace it :)
        }
    }