将属性添加到单个DatagridViewCell

时间:2015-02-23 21:37:59

标签: c# winforms datagridview

我正在进行一个基础控制是DatagridView的项目,其中显示了生产数量的数量,每一行都是生产过程中的bach,而每一列都是bash中的一种产品。

每个Bash都有时间完成整个过程,当时间结束时,行中的单元格必须着色,然后用户可以根据需要为每个产品添加更多时间。

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

  1. bash产品的状态(int)。
  2. 以分钟为单位的延长时间(默认为int 0)。
  3. 所以我用这种方式创建自己的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].TiempoExtra 要么 dgView.Row[0].Cell[2].Estado获取我需要了解细胞如何整理的信息?

    感谢您的帮助

2 个答案:

答案 0 :(得分:1)

为什么不使用属性标记每行必须存储的标记 批量信息,比您可以轻松检索

Por que no usas la propiedad Tag que tiene cada row 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,则需要按照这种方式执行此操作

// ===>您无法直接指定值,因为tag属性是一个对象,因此您需要在下面执行这样的转换     BatchInfo SelectedBatchInfo =(BatchInfo)miDataGrid.SelectedRows(0).Tag;

//==>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;

答案 1 :(得分:0)

Si ya extendistes la Clase DataGrid por que no agregarle unas propiedades mas como

如果您已经扩展了DataGrid类,为什么不像这样添加新属性

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