RadDataGrid programmatical new row insertion not working

1 Answer 114 Views
DataGrid
William
Top achievements
Rank 1
William asked on 23 May 2021, 09:38 PM | edited on 23 May 2021, 09:51 PM

I need to create a RadDataGrid where rows are dynamic custom control coming from API. I had asked a question before and it's helped me a lot. But currently, I cannot work more than one row. The first-row creation is very fine it's working so nicely with my custom controls, but not for the rest rows.  

If I use Label it works, but not in a dynamic custom control.

 


private void AddHeaderRowNew(IEnumerable<string> headers)
        {


            Feature newTree = (this.Parent as Home).GetNewTree();

            Dto.FeatureContainer container = Controls.ControlFactory.GetContainer(this.Template, newTree, this.Olp.AppProject.Layout.MetaTree,
                FeatureState.New,
                this.TreeRules,
                ApiClient.Models.RuleTarget.Tree);

            var views = container.GetViews(this.SimpleTreeElementIds);

            int index = 0;

            var columnWidth = Application.Current.MainPage.Width / headers.Count();

            foreach (string header in headers)
            {
                var view = views[index];
                columns.Add(header);
                this.dataGrid.Columns.Add(new DataGridTemplateColumn { SizeMode = DataGridColumnSizeMode.Fixed, Width = columnWidth, HeaderText = header, Name = header, CanUserFilter = false, CanUserGroup = false, CanUserSort = false, CanUserEdit = true, HeaderStyle = new DataGridColumnHeaderStyle { BorderThickness = new Thickness(0) }, CellContentTemplate = GetTemplate(view) });
                index++;
            }
        }


        public DataTemplate GetTemplate(View view) //working only for first row
        {
            return new DataTemplate(() =>
            {
                var stackLayout = new StackLayout();

                stackLayout.Children.Add(view);

                return stackLayout;
            });
        }

        public DataTemplate GetTemplate(string text) //working fine
        {
            return new DataTemplate(() =>
            {
                var stackLayout = new StackLayout();

                Label oLabel = new Label()
                {
                    BackgroundColor = Color.Transparent,
                    TextColor = Color.Black,
                    VerticalOptions = LayoutOptions.FillAndExpand,
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                };

                //oLabel.SetBinding(Label.TextProperty, psPropertyName);
                oLabel.Text = text;

                stackLayout.Children.Add(oLabel);

                return stackLayout;
            });
        }
Have any idea to solve it? 
Yana
Telerik team
commented on 27 May 2021, 06:12 AM

I would need more details on the missing GetViews method, can you give an example of a View that is passes as a parameter to the GetTemplate(View view)?
William
Top achievements
Rank 1
commented on 31 May 2021, 05:35 PM

public IEnumerable<View> Views;

public IList<View> GetViews(IEnumerable<int> elementIds)
{
List<View> selected = new List<View>();
foreach (int i in elementIds)
{
selected.AddRange(this.Views.Where(x => (x as Controls.IF4View).ElementId == i));
}
return selected;
}

1 Answer, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 02 Jun 2021, 10:39 AM

Hello William,

Thank you for the follow-up.

The problem comes from the fact that Controls.IF4View instances are created only once and are reused inside the  GetTemplate(View view)  method for all the items from the DataGrid ItemsSource - that's the reason they're rendered only for the first item. 

You would need to rewrite the GetTemplate method to receive a different parameter and create IF4View instance inside it for each TemplateColumn for each item (in the same way as in GetTemplate(string text)  the Label is created), for example:

public DataTemplate GetTemplate(int Id)
{
    return new DataTemplate(() =>
    {
        var view = new IF4View() { ElementId = Id };
        var stackLayout = new StackLayout();

        stackLayout.Children.Add(view);

        return stackLayout;
    });
}

I hope I was of help.

Regards,
Yana
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
DataGrid
Asked by
William
Top achievements
Rank 1
Answers by
Yana
Telerik team
Share this question
or