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

 

  • 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

  • 785
    posted
    One approach I've tried to solve my problem of needing to be able to dynamically add columns to a databound xamDataGrid is to bind to a DataTable. At startup, I hard-code some data into the DataTable. Then I provide a button which, when clicked, is supposed to add a new column and put random numbers in the new column. I then unbind and rebind the xamDataGrid. Oddly, I see only the original columns. My newly-added column does not show up. Here's the code: MyDataTable.cs -------------- using System.Data; namespace XamDataGridBoundToDataTable { class MyDataTable : DataTable { static MyDataTable() { Instance = new MyDataTable(); } public static MyDataTable Instance { get; private set; } } } Window1.xaml.cs --------------- using System; using System.Windows; namespace XamDataGridBoundToDataTable { /// /// Interaction logic for Window1.xaml /// public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { m_myDataGrid.DataSource = MyDataTable.Instance.Rows; } private void m_addColumnButton_Click(object sender, RoutedEventArgs e) { int ColNum = MyDataTable.Instance.Columns.Count + 1; string columnName = string.Format("Column {0}", ColNum); MyDataTable.Instance.Columns.Add(columnName, typeof(int)); MyDataTable.Instance.Rows[0][columnName] = m_rand.Next(100); MyDataTable.Instance.Rows[1][columnName] = m_rand.Next(100); m_myDataGrid.DataSource = null; m_myDataGrid.FieldLayouts.Clear(); m_myDataGrid.DataSource = MyDataTable.Instance.Rows; } private readonly Random m_rand = new Random(); } } App.xaml.cs ----------- using System.Windows; namespace XamDataGridBoundToDataTable { /// /// Interaction logic for App.xaml /// public partial class App : Application { private void HandleStartup(object sender, StartupEventArgs e) { MyDataTable.Instance.Columns.Add("Column 1", typeof(int)); MyDataTable.Instance.Columns.Add("Column 2", typeof(int)); MyDataTable.Instance.Columns.Add("Column 3", typeof(int)); MyDataTable.Instance.Rows.Add(new object [] {243, null, 158}); MyDataTable.Instance.Rows.Add(new object[] {17, 42, 144}); } } } Can anybody fathom a reason why I'm still seeing only the original columns even after unbinding and rebinding? Thanks, Dave