Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
785
Data Binding With a Variable Number of Columns
posted

Hello All,

I have a need to have a variable number of fields in a data-bound xamDataGrid.

The following code will (hopefully) convey what I'm trying to accomplish.

// This class represents a data item with a variable number of fields.
// When the set of fields in one data item changes, all other data
// items will follow suit. In other words, Attributes.Keys will be
// the same for every instance of DataItem (except while in mid-update).

class DataItem
{
   // The string represents the field name and the int represents the
   // field value.
   public Dictionary<string, int> Attributes { get; set; }
}

// Bind to this
class DataItemCollection : List<DataItem>
{
}

Again, I desire to have the keys of DataItem.Attributes display as simple fields, not to have DataItem.Attributes display as a single expandable field.

Can the above be made to work? If not, is there any other way I can obtain the same effect?

Thanks,

Dave

 

Parents
No Data
Reply
  • 260
    Verified Answer
    Offline posted

    I implemented a variable number of columns by doing:

     

    The datagrid .DataSource is set to a List<RowData>

     

    public class RowData

    {

    /// <summary>

    /// Array of CellData, makes up everything shown in a row on the grid

    /// </summary>

    private CellData[] _rowData;

    public CellData[] Cells

    {

    get { return _rowData; }

    set { _rowData = value; }

    }

    }

     

    Then the field layout is generated from fields setup like:

    UnboundField field = new UnboundField();

    field.DataType = typeof(object);

    field.BindingPath = new PropertyPath("Cells[" + index + "].Value");

     

    CellData.Value returns an object, based on what is going to be shown in the column it sets up EditorType and EditAsType according to what is actually inside the object

Children
No Data