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
565
Dynamically added columns - header shows up, values don't
posted

I've got  a XamDataGrid where I'm adding columns dynamically.  The columns add themselves directly, and the headers are correct, but the data cell values are blank.  I can't figure out how to get the binding right.

I've read through quite a few other similar threads, but none of them seemed to fit my situation right.

Code snippets are below.  I tried to cut out superfluous detail; apologies for any artifacts from those edits.

The interesting part is the AddColumn() method at the end.  I first tried using a Field type instead of the UnboundField, but I couldn't find an appropriate method or property to set binding.  Following an example in another thread, I tried the UnboundField as you can see below, but it's not right.  (I hope it's not something silly like an incomplete BindingPath, but it's possible.)

Any help would be much appreciated.

 

WPF Code:

<igDP:XamDataGrid.FieldLayouts>
    <igDP:FieldLayout>
        <igDP:FieldLayout.Fields />
    </igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>

Code-Behind:

// storage class for each row
public class QBRow
{
    public List<string> Items { get; private set; }

    public QBRow(string rowHeader)
    {
        Items = new List<string>();
    }
}

// ctor for the window that contains the XamDataGrid
public QuotesBoard()
{
    Rows = new BindingList<QBRow>();

    InitializeComponent();
    grid.DataSource = Rows; //grid is the XamDataGrid, of course
}

// method that adds the column
private void AddColumn(string r0, string r1, string r2)
{
    Rows[0].Items.Add(r1);
    Rows[1].Items.Add(r2);
    Rows[2].Items.Add(r3);

    int index = Rows[0].Items.Count - 1;

    Infragistics.Windows.DataPresenter.UnboundField f =
        new Infragistics.Windows.DataPresenter.UnboundField();
    f.Name = "newCol_"+index.ToString();
    f.DataType = typeof(string);
    f.BindingPath = new PropertyPath("Items["+index+"].Value");
    //Also attempted: "Items[0].Value"
    grid.FieldLayouts[0].Fields.Add(f);
}

Parents
No Data
Reply
  • 54937
    Offline posted

    gbirchmeier said:
        Rows[0].Items.Add(r1);
        Rows[1].Items.Add(r2);
        Rows[2].Items.Add(r3);

        int index = Rows.Items.Count - 1;

        Infragistics.Windows.DataPresenter.UnboundField f =
            new Infragistics.Windows.DataPresenter.UnboundField();
        f.Name = "newCol_"+index.ToString();
        f.DataType = typeof(string);
        f.BindingPath = new PropertyPath("Items["+index+"].Value");
        //Also attempted: "Items[0].Value"
        grid.FieldLayouts[0].Fields.Add(f);

    I could be mistaken but it looks like the issue is the value of the "index" variable. You are using the # of Rows (i.e. the number of data items) and not the # of items in the Items collection of each row. That being said if each Row has a different # of items then you really won't be able to do this using an Unbound field with a Binding(Path). The only way for that then would be to handle the InitializeRecord and set the cell's value to whatever you want to show. Alternatively if for example you wanted to show the last item in the Items collection then you might expose another property on your QBRow (e.g. "LastRow") and you send the property change notification when the collection changes (and therefore the value fot he LastRow has changed).

Children