2013-05-20 102 views
2

我有一个测试程序构建一个DataTable(26行6列)作为DataGridView的数据源。
可以通过选择单元格并从3个NumUpDown控件之一输入值来编辑数据网格。
每个控件都有特定的颜色。为了保存信息,我将表格保存在用户设置中。
我将每个单元格的颜色信息保存在额外的表格单元格中。
问题:当我显示数据网格时,它会读取表格中的值,但应该基于附加单元格信息以编程方式添加的单元格颜色不会显示。
(我可以在诊断的细胞得到正确的颜色看,但它不会在DataGrid中显示。
貌似datagird有一些颜色替代(?)。
附件为完整的工作代码。
看到代码的详细信息。
任何人都可以是一种运行这一点,并找到为什么颜色不会在DataGrid显示? 感谢。DataGridView单元格颜色变化

using System; 
using System.Data; 
using System.Drawing; 
using System.Web.UI.WebControls; 
using System.Windows.Forms; 

//This is a test program to do the following: 
//1. Build an empty data table of 26 rows by 6 columns. 
//2. The Table is bind (as DataSource) to a DataGridView. 
//3. The DataGrid Colomns 0-3 can be filled with decimals by one of 3 NumericUpDown controls. 
//3. For each cell in cols 0-2 there is one in cols 3-5 that holds info which NumUpDn was used. 
//4. Each NumUpDn has specific color. The corresponding cells have the same backcolor. 
//5. When the program starts, it builds the table from the Settings, and displays the table in the datagrid. 
//6. The cells in cols 0-2 are supposed to get the colors ad indicated in cols 3-5, but they do not. 
//To run, make a WinForm with a datagrid 'fareGrid' and 3 NumUpDn controls with background colors. 


namespace DataGridViewTest 
{ 
public partial class Form1 : Form 
{ 
    DataTable fareTable = new DataTable(); 

    int rowIndex; 
    int colIndex; 

    public Form1() 
    { 
     InitializeComponent(); 
     buildFareTable("fareTable"); 
     // Load the table (if exists) 
     if (Properties.Settings.Default.fareTable != null) 
      fareTable = Properties.Settings.Default.fareTable; 
     paintFareTableCells(fareTable); 

    } 

    //Build the initial data table of 26R X 6C 
    //Columns 3-5 are used to hold the color information of cols 0-2 
    public void buildFareTable(string fareTableName) 
    { 
     fareTable.TableName = fareTableName; 
     fareTable.Columns.Add("Weekday", typeof(Decimal)); 
     fareTable.Columns.Add("HalfDay", typeof(Decimal)); 
     fareTable.Columns.Add("Weekend", typeof(Decimal)); 

     //Adding 3 indexing columns that will hold the tables cell group 
     fareTable.Columns.Add("IWeekday", typeof(String)); 
     fareTable.Columns.Add("IHalfDay", typeof(String)); 
     fareTable.Columns.Add("Iweekend", typeof(String)); 

     for (rowIndex = 0; rowIndex < 26; rowIndex++) 
     { 
      fareTable.Rows.Add(); 
     } 
    } 

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

     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; 
      } 
     } 
    } 

    //Update the cells by 3 NumUpDn controls. 
    private void numericUpDownFareHigh_ValueChanged(object sender, EventArgs e) 
    { 
     editFareTableCells(sender); 
    } 
    private void numericUpDownFareMed_ValueChanged(object sender, EventArgs e) 
    { 
     editFareTableCells(sender); 
    } 

    private void numericUpDownFareLow_ValueChanged(object sender, EventArgs e) 
    { 
     editFareTableCells(sender); 
    } 

    public void editFareTableCells(Object sender) 
    { 
     foreach (DataGridViewCell cell in fareDataGrid.SelectedCells) 
     { 
      if (cell.ColumnIndex < 3) 
      { 
       cell.Value = ((NumericUpDown)sender).Value; 
       cell.Style.BackColor = ((NumericUpDown)sender).BackColor; 
       fareDataGrid[cell.ColumnIndex + 3, cell.RowIndex].Value = ((NumericUpDown)sender).Tag; //Uses cols 3-5 to hold cost level info. 
      } 
     } 
    } 

    //Use settings to preserve the data 
    private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     Properties.Settings.Default.fareTable = this.fareTable; 
     Properties.Settings.Default.Save(); 
    } 
} 
} 

回答

0

在你paintFareTableCells,很可能的情况下语句下降到default和因此不设置任何值。用断点确认,并相应修改列索引。

我希望这会有所帮助。

+0

谢谢,但事实并非如此。 – samtal

+0

谢谢,但事实并非如此。 正如我所指出的那样,我使用断点进行了测试,并添加了一行读取每个单元格中的颜色的行。他们都很好。 问题是,当我关闭并重新运行程序时,表格没有问题,所有命令都被执行,单元格具有正确的值,但单元格背景色仍然全是白色,但我可以在属性中看到他们是有色的。 它看起来像程序中的某些东西在设置之后和网格显示之前移除颜色。 有什么想法?谢谢 – samtal

+0

'DataGridView'中单元格的'Backcolor'在应用程序重新启动之间不会持续存在。您可能已经为单元格值调用了保存机制,但您需要重新对单元格着色。 – MoonKnight

1

颜色更改代码必须位于事件处理程序中;具体来说就是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; 
     } 
    } 
} 

单元现在根据需要根据值更改颜色。

顺便说一句,不要忘记订阅到事件处理程序在你的构造:

fareDataGrid.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(fareDataGrid_DataBindingComplete); 
0

不幸的是,我不知道为什么你的细胞没有被描绘成他们应该是,但我最好的猜测会是你在处理绘画事件之前试图改变属性的事实。 我设法改变事件的颜色,正如Joe Sisk所建议的那样,它对我来说工作正常。我希望它能帮助你。所不同的是我用CellPainting事件,这里是我所做的短版,我希望它可以帮助你弄清楚自己休息...

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 :) 
     } 
    } 

我希望这对你的作品?