【问题标题】:Adding properties to single DatagridViewCell向单个 DatagridViewCell 添加属性
【发布时间】:2022-05-11 22:46:24
【问题描述】:

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

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

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

  1. bash 产品的状态 (int)。
  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].TiempoExtra 或者 dgView.Row[0].Cell[2].Estado 获取我需要知道单元格将如何着色的信息?

感谢您的帮助

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

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

    structure BatchInfo{
    //===>Informacion de tu batch aqui.
    //===>Add here fields of information of your batch
    ...
    }
    
    //===>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
    }
    
    /===>If you want to retrieve the batchInfo from the row tag property you need to do it like this way
    
    //===>You can not assign the the value directly because tag property is an object, so you need to do a cast like this way below
    BatchInfo SelectedBatchInfo = (BatchInfo)miDataGrid.SelectedRows(0).Tag;
    
    //==>And 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;
    

    【讨论】:

    • 用英语回答会帮助很多用户。对于访问该问题的非英语人士来说,这几乎没有用。请参阅此meta 帖子进行讨论
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-11
      • 2018-10-28
      相关资源
      最近更新 更多