将属性添加到单个DatagridViewCell

将属性添加到单个DatagridViewCell

问题描述:

我在一个项目中,其中基本控件是一个DatagridView,在那里显示生产数量的数量,每行是生产过程中的巴赫,每一列是一个产品类型庆典。将属性添加到单个DatagridViewCell

每个Bash都有时间来完成过程,当时间结束时,行中的单元格必须被着色,然后用户有能力为每一个产品添加更多时间(如果需要的话)。

所以我的建议是增加每个Cell对象的两个属性

bash的产品(INT)的
  1. 状态。
  2. 以分钟为单位的延长时间(默认为int 0)。

所以我创造我自己的DataGridViewCell这样

public class PedidosCell : DataGridViewCell 
{ 
    private int _estado; 
    private int _tiempo; 

    public int Estado 
    { 
     get { return _estado; } 
     set { _estado = value; } 
    } 

    public int TiempoExtra 
    { 
     get { return _tiempo; } 
     set { _tiempo = value; } 
    } 
} 

之后,我创建了一个使用PedidosCell

public class PedidosColumn : DataGridViewColumn 
{ 
    public PedidosColumn() 
     : base(new PedidosCell()) 
    { 
    } 

    public override DataGridViewCell CellTemplate 
    { 
     get 
     { 
      return base.CellTemplate; 
     } 
     set 
     { 
      // Ensure that the cell used for the template is a PedidosCell. 
      if (value != null && 
       !value.GetType().IsAssignableFrom(typeof(PedidosCell))) 
      { 
       throw new InvalidCastException("Must be a PedidosCell"); 
      } 
      base.CellTemplate = value; 
     } 
    } 

这里的问题开始科拉姆,因为如果我调用构造函数

PedidosColumn col = new PedidosColumn(); 

特性

col.CellTemplate.TiempoExtra

不存在;并且它因为置换器CellTemplate被返还原物CellTemplate

但我怎么能做到这一点(如果可能)做一个简单的dgView.Row[0].Cell[2].TiempoExtradgView.Row[0].Cell[2].Estado得到我需要知道如何细胞会colorated的信息很明显?

感谢对帮助

为什么你不使用属性Tag这是每行有存储 的批次信息,比你可以轻松地检索

POR阙无美联钢构拉propiedad标签阙tiene CADA行para almacenar la informacion de cada registro y asi poder recuperarla facilmente。

structure BatchInfo{ 
//===>Informacion de tu batch aqui. 
//===>Add here fields of information of your batch 
... 
} 


//===>Llenar la informacion de cada row con la informacion adicional 
//===>You can fill each datagrid row tag property with the batch info like this  
foreach(DataGridViewRow iRow int miDataGrid.Rows){ 
    iRow.Tag = new BatchInfo("BatchName");//===>Create a new object of your structure 
} 

//===>Para recuperar la informacion del batch solo tendrias que hacer esto. 

/===>如果你想从你需要做的像这样的行标签属性检索batchInfo

// ===>您不能分配的值直接因为标签属性是一个对象,所以您需要按照以下方式进行投射。 BatchInfo SelectedBatchInfo =(BatchInfo)miDataGrid.SelectedRows(0)。标签;

//==>y para colorear alguna celda del grid. 
//==>Ans if you want add color to specific cell do it this way 
miDataGrid.SelectedRow(0).Cell("MiColumna").style.BackColor = Color.Navy; 
miDataGrid.SelectedRow(0).Cell("MiColumna").style.Forecolor = Color.WhiteSmoke; 
+0

即试图在第一,但我用一个int [排列],但我难以设定为提取整个阵列并将其保存到一个新的实例,在那里他保持在由索引属性所需要的属性,然后当建立了AR Reglo返回标签。但是,我认为有可能是做得到它,为乳白色小区物业 – 2015-02-23 22:35:34

+0

只实现你的建议更自然的方式,我觉得效果很不错,比预期要好,我会用结构,它是方便提取并保存的特性,给予灵活地添加一些更多的deseee,谢谢。在英语回答 – 2015-02-23 23:54:31

+0

将帮助大量的用户。这是几乎无用的非英语的扬声器谁正在访问的问题。看到这个[元](http://meta.stackoverflow.com/questions/262014/how-to-deal-with-non-english-posts)后进行讨论 – Junaith 2015-02-25 08:50:15

extendistes

如果DataGrid类 为什么不添加属性更像

,如果你已经扩展DataGrid类的,为什么你不只是一个新的属性添加到像这样

BatchInfo GetSelectedBatchInfo{ 
    get{ 
     if(this.SelectedRows.Count > 0){ 
      return (BatchInfo)this.SelectedRows(0).Tag; 
     }else{ 
      return null; 
     } 
    } 
}