2013-05-15 55 views
0

我有一个DataTable,在所有单元格中都保留Decimal值。 在该表中,我需要在每个组的单元格中具有相同值的3个值组,这些值分布在表中。DataTable中的组单元格

使用3个NumUpDown控件,我希望控制表格中每个组单元格中的值。

我可以很容易地访问和控制表中每个单元格的值(MyTable.Rows[rowIndex][colIndex] = numUpDown.Value),但是我没有找到任何方法来定义这些组并将它们彼此区分开来。

ASAIK,表格单元格没有可用于区分它们的属性。 一个选项可能是构建另一个相同的索引表,它将保存分组信息,但我正在寻找更直接的方法。

(可能有一个解决方法是使用组中实际的初始相同值,但这远非安全)。 任何想法将不胜感激! 谢谢

+1

您的问题没有明确变电工能源成本。这些行如何相互关联?表中有用于标识组的表中的外键列吗? –

+0

我无法引用行,但只能引用单个单元格。每行或每列可以容纳来自这三组中的任何一组的单元。 我没有索引列,因为这样的列不能指向一个单元格(除非使用复杂的编号算法),但是您的评论使我想到为每个表列添加索引列,而不是构建额外的索引表。 我无论如何,我仍然希望有人会带着一个简单的魔法的好主意。 – samtal

+0

我假设表格是从数据库中填充的。向我们展示表格和列的类型将是一个好的开始。您还应该向我们显示加载它的代码。 –

回答

0

正如我的建议,我通过在表中添加额外的列来解决这个问题。 对于每个单元格,都会添加带有颜色(或其他)信息的单元格。 当datagrid打开时,它读取添加的单元格,并根据额外单元格中的信息决定基本单元格的颜色。下面是我设计的一个测试程序的完整工作代码: (运行时,用DataGridView的'fareGrid'和3个NumUpDown控件构建一个WinForm) (我仍然有问题,颜色不起作用在代码中),但我将它作为一个单独的问题发布)。 这是一个程序的一小部分,计算每使用时间(一天的一周,小时,季节)

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 the 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. 


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(); 
    } 
    } 
} 
相关问题