Cesar Romeroo
Cesar Romeroo

Reputation: 193

Adding properties to single DatagridViewCell

I'm on a project where base control is a DatagridView, in there is displaying numbers of production quantities, whish every row is a bach on the production process and every column is a type of product in the bash.

Every Bash has a time to acomplish to finalize the process, when time is ended, the cell in the row must be colored, then the user has the ability to add more time if needed to every single product.

So my proposal was adding to every Cell object two properties

  1. State of the bash product (int).
  2. Extended time in minutes (int 0 default).

So I create my own DataGridViewCell this way

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

After that I created the colum that uses 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;
        }
    }

The problem starts here because if i call a constructor

PedidosColumn col = new PedidosColumn();

the propertie

col.CellTemplate.TiempoExtra

doesn't exist; AND it's obvious because the overrider CellTemplate is returning the original CellTemplate

But how can i do it (if possible) to make a simple dgView.Row[0].Cell[2].TiempoExtra or dgView.Row[0].Cell[2].Estado to get the information I need to know how the cell will be colorated?

Thank's For the help

Upvotes: 0

Views: 759

Answers (2)

MrAlex6204
MrAlex6204

Reputation: 44

Why you not use the property Tag that's every rows has to storage the batch information, than you can retrieve easily

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;

Upvotes: 1

MrAlex6204
MrAlex6204

Reputation: 44

If you already extended the DataGrid Class why you don't just add a new property to it like this

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

Upvotes: 0

Related Questions