Dynamically generate TableView from C# classes

SiddySiddy Member ✭✭

I want to generate a tableview, where the sections and table would be generated from the values retrieved from the database.

I followed this link, and followed the approach which says how to create a custom table view, but I just keep getting blank page.

Following is my code:
public MainPage()
{
List titles = new List()
{
"First Section", "Second Section"
};
List models = new List()
{
new HomePageListViewModel
{
ID = 1, Lists = "Label1", title = "First Section"
},
new HomePageListViewModel
{
ID = 2, Lists = "Label2", title = "First Section"
},
new HomePageListViewModel
{
ID = 3, Lists = "Label3", title = "Second Section"
},
new HomePageListViewModel
{
ID = 4, Lists = "Label4", title = "Second Section"
}
};

        Categories cat = new Categories();

        foreach (var sections in titles)
        {
            var labels = from s in models
                         where s.title == sections
                         select s;
            cat.Add(sections, labels.ToList());                
        }
        //BindingContext = cat;
        InitializeComponent();
    }

The table view logic is provided in the above link.

Can anyone please guide me how to achieve the above through c#, or anything with my approach here?.

Thanks In Advance!!!..

Best Answer

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @Siddy

    There is nothing in your code that builds a UI. Your code populates a Categories instance (whatever that is - no code provided), but nothing links the UI to that Categories instance.

  • SiddySiddy Member ✭✭

    Did you check the link given above in the problem statement. Anyways this should have:

    https://stackoverflow.com/questions/45666086/how-can-i-add-a-viewcell-with-a-grid-to-a-tableview-in-c-sharp

  • JohnHardmanJohnHardman GBUniversity mod
    edited May 10

    @Siddy said:
    Did you check the link given above in the problem statement. Anyways this should have:

    https://stackoverflow.com/questions/45666086/how-can-i-add-a-viewcell-with-a-grid-to-a-tableview-in-c-sharp

    Yes, I looked at that. However, without you posting the C# and XAML that you are using, I (and anybody else looking) can only guess as to what bits you are actually using, particularly given that the StackOverflow site contains more than one option.

    You are populating an instance of Categories, which is a plain old C# object. You may or may not be using a binding to AllCategories, which populates another instance of Categories. Whether you kept the body of the constructor from the StackOverflow page I don't know. Whether you did or not, your local instance of Categories will not be used if you are using AllCategories as shown on StackOverflow.

    Post all of your related C#/XAML. Otherwise, any answers are educated guesses

  • SiddySiddy Member ✭✭

    I am trying to play around with some code, and got some code working, but the issue is I only get last "section" populated rather than all the section. So again made some code changes.

    Following is my new code logic:

       TableView mainTable = new TableView();
    
            foreach (var sections in titles)
            {
                var section = new TableSection() { Title = sections };           
    
                //var root = new TableRoot();
                var layout = new StackLayout();
    
                var labels = from s in models
                             where s.title == sections
                             select s;                
    
                foreach (var label in labels)
                {
                    var formattedString = new FormattedString();                 
    
                    var span = new Span { Text = label.Lists };
                    span.GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(async () => await DisplayAlert("Tapped", "This is a tapped Span.", "OK")) });                    
    
                    layout.Children.Add(new Label { FormattedText = formattedString });
                    mainTable.Root = new TableRoot()
                    {
                        new TableSection(sections)
                        {
                            new ViewCell() {View = layout}
                        }
                    };
                }                                               
            }
            Content = mainTable;
    

    Any help you can provide based on the above code I posted. This is in continuation with the code I previously posted.

  • JohnHardmanJohnHardman GBUniversity mod

    @Siddy said:
    This is in continuation with the code I previously posted.

    Post the whole lot as it stands now

Sign In or Register to comment.