lundi 4 mai 2015

Adding UserControls in TableLayoutPanel Row's

Seems like TableLayoutPanel in C# going up-top of my head.
I have a UserControl to populate user data from SQLite database in a nicer way.

Now i have a WindowsForm called "Dashboard.cs" within that i have a TableLayoutPanel called "dashboardTableItemsWrapper".

Now my plan is to populate as many UserControl in that TableLayoutPanel as many time i have data from my database.
simple, huh?

To do this i have a method in "Dashboard.cs" called updateUserLists() which actually fetch the database results and get called from the constructor, then run a loop and everytime inside the loop it will instantiate a new object of my UserControl and append that into the TableLayoutPanel.
Here is the method i have code:

private void updateUserLists()
    {
        String userID = "1"; // i'll think about this later.
        Database db = new Database();
        using (db.connection)
        {
            try
            {
                db.connection.Open();
                SQLiteCommand query = new SQLiteCommand("SELECT * FROM list WHERE author_id = '" + userID + "'", db.connection);
                using (System.Data.SQLite.SQLiteDataReader reader = query.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        SingleItemUserControl usrItem = new SingleItemUserControl();
                        usrItem.itemTitle.Text = reader["title"].ToString();
                        usrItem.itemDescription.Text = reader["detail"].ToString();
                        usrItem.itemWebLink.Text = reader["weblink"].ToString();
                        usrItem.itemTimeDate.Text = reader["datentime"].ToString();
                        dashboardTableItemsWrapper.Controls.Add(usrItem);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }


Once i run this code it gives no error except a empty dashboard.
What is your idea?

Aucun commentaire:

Enregistrer un commentaire