Elizah Posia
Elizah Posia

Reputation: 1

Binding data from SQL Table to custom list created using user control in C#

I am working a project and i need to display on a form (frmCaterlogs) a list of items. I have successfully implimented a custom list using a user control & FlowLayOutPanel. However now i am stuck on how i can bind my caterlogs that sits on a sql database to my custom list: Here is my code. on the custome_list(CatList), i have 4 controls, Id, Titile, Description, Icon stored in database as binarydata in the sqldb. I am lost on how i can bind data to the custom list control that can look thought all the records in my database. Thanking you all in advace for your kind advices.

    private void PopulateCatelog()// This code is triggered when frmcaterlogs loads.
    {
        int l;
        string query = "SELECT * from ServicesCaterlogs";
        SqlConnection con = new SqlConnection(cn);
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        ListView Items = new ListView()
        sda.Fill(dt);
        l = dt.Rows.Count;
        foreach(DataRow dr in dt.Rows) // This is where i am stuck
        {
            CatList iList = new CatList(dr["Item"].ToString());
            iList.Title = dr;
            
        }
        CatList[] ListItem = new CatList[l];
        //Loop though to check each item 
        for (int i = 0; i < ListItem.Length; i++)
        {
            ListItem[i] = new CatList();
            ListItem[i].Title = fetch data for a list;
            ListItem[i].Message = "fetch data for a lis";
            ListItem[i].icon = Resources.Warning;
            ListItem[i].IconBackground1 = Color.DarkGoldenrod;

            if (FlowLayoutPanel.Controls.Count < 0)
            {
                FlowLayoutPanel.Controls.Clear();
            }
            else
            {
                FlowLayoutPanel.Controls.Add(ListItem[i]);
            }
          
        } 

Upvotes: 0

Views: 1309

Answers (1)

Elizah Posia
Elizah Posia

Reputation: 1

I got the desired result after altering the code as seen below

private void FrmCatalogs_Load(object sender, EventArgs e)
        {
            PopulateCatelog();
        }
        private void PopulateCatelog()
        {
            //Populate your listitem here 
            int l;
            string query = "SELECT * from ServicesCaterlogs";
            SqlConnection con = new SqlConnection(cn);
            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            
            sda.Fill(dt);
            l = dt.Rows.Count;
            foreach(DataRow dr in dt.Rows)
            {
                ListViewItem item = new ListViewItem(dr["Item"].ToString());
                ListViewItem des = new ListViewItem(dr["Item_Description"].ToString());


                CatList[] ListItem = new CatList[l];


                for (int i = 0; i < ListItem.Length - l +1 ; i++)
                {
                    ListItem[i] = new CatList();
                    ListItem[i].Title = item.Text;
                    ListItem[i].Message = des.Text;
                    ListItem[i].icon = Resources.Warning;
                    ListItem[i].IconBackground1 = Color.DarkGoldenrod;

                    if (FlowLayoutPanel.Controls.Count < 0)
                    {
                        FlowLayoutPanel.Controls.Clear();
                    }
                    else
                    {
                        FlowLayoutPanel.Controls.Add(ListItem[i]);
                    }

                }

            }
            
        }

Upvotes: 0

Related Questions