Index binding

6 posts, 1 answers
  1. Hakki
    Hakki avatar
    47 posts
    Member since:
    Sep 2012

    Posted 29 Oct 2012 Link to this post

    Hello,

    I am setting up a grid for incoming data that is categorical. I do not know how many categories said data will have so it is dynamic. So I have set up something like this:

    //if column got sent, put them in the grid alongside visit
                            Debug.Assert(Phpdata != null, "Phpdata != null");
                            if (Phpdata.ColumnCount != 0)
                            {
                                //populate grid for categorical data
                                Debug.Assert(Phpdata.Visit != null, "Phpdata.Visit != null");
                                for (var i = 0; i < Phpdata.Visit.GetLength(0); i++)
                                {
                                    var ds = new Datas
                                        {
                                            VisitColumn = Phpdata.Visit[i],
                                            nColumn = Phpdata.n[i],
                                            PercentageColumn = Phpdata.Percentage[i]
                                        };
     
                                    MyData.Add(ds);
                                }
     
                                //for each column create column n and column percentage
                                Debug.Assert(Phpdata != null, "Phpdata != null");
                                for (var c = 0; c < Phpdata.ColumnCount; c++)
                                {
                                    var totalcolumn = new GridViewDataColumn
                                        {
                                            DataMemberBinding = new Binding("nColumn"),
                                            Header = Phpdata.ColumnName[c] + " [n]",
                                            UniqueName = "TotalColumn" + c
                                        };
     
                                    var percentageColumn = new GridViewDataColumn
                                        {
                                            DataMemberBinding = new Binding("PercentageColumn"),
                                            Header = Phpdata.ColumnName[c] + " [%]",
                                            UniqueName = "PercentColumn" + c
                                        };
     
                                    RadGrid.Columns.Add(totalcolumn);
                                    RadGrid.Columns.Add(percentageColumn);
                                }

    The problem is, this repeats the first data for each column. So I have tried something like this:

    //same part as above just with binding changed
    var percentageColumn =
    new GridViewDataColumn
        {
            DataMemberBinding = new Binding("PercentageColumn[" + c + "]"),
            Header = Phpdata.ColumnName[c] + " [%]",
            UniqueName = "PercentColumn" + c
        };

    But this return blank.

    I have also tried:
    DataMemberBinding = new Binding("Phpdata.Percentage[" + c + "]")
    Which seems to have worked for Header but that too returned blank.

    So how can I get the index of PercentageColumn, i.e: PercentageColumn[0] for first column and PercentageColumn[1] for second column etc so it binds to new data.

    I am not even sure if I am going about the right way of handling dynamic columns but it made sense to me and I feel I am just missing the right syntax?

    Thanks for any help/ideas!
  2. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1219 posts

    Posted 01 Nov 2012 Link to this post

    Hello,

    Basically, this seems to be syntactically valid. Would you please check your Output for BindingExpressionPath errors? Would it be possible for you to mimic this behavior in a simple repro project that we can debug on our side?

    Greetings,
    Ivan Ivanov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. Hakki
    Hakki avatar
    47 posts
    Member since:
    Sep 2012

    Posted 07 Nov 2012 Link to this post

    Hello Ivan,

    Thanks for your interest in helping me resolve this issue. You can find a sample project here:
    ~faulty file link edited out ~

    Just to clarify grid should show:
    - "Yes n" to be 6, "Yes %" to be 54.5
    - "No n" to be 5, "No %" to be 45.5

    Thanks again and looking forward to your response,
    Hakki
  4. Hakki
    Hakki avatar
    47 posts
    Member since:
    Sep 2012

    Posted 07 Nov 2012 Link to this post

    My apologies for the faulty file above, here is a working one:

    https://www.dropbox.com/s/57jwskc2q7ncw9f/DynamicColumns.zip
  5. Answer
    Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1219 posts

    Posted 12 Nov 2012 Link to this post

    Hi,

    I have succesfully managed to run your project. Would you please shed some more light on the follwoing matter: you are binding the RadGridView to an ObservableCollection<MyData> that has a single entry as seen on the attached "Watch" screenshot. Then you are binding two different columns to one and the same property. Apparently the same value is displayed in both columns. Shouldn't you use Collection types to define the DataView properties? I have modifed your project a bit. The updated version is attached for your reference.

    Greetings,
    Ivan Ivanov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Hakki
    Hakki avatar
    47 posts
    Member since:
    Sep 2012

    Posted 20 Nov 2012 Link to this post

    Sorry for the delay just got around to actually testing this out. No light to shed I'm afraid, just a terrible oversight on my part there.

    Your example project works perfectly and I have successfully applied it to my full project. Thanks a lot.
Back to Top