Home > Back-end >  Load data into flow layout panel in winforms
Load data into flow layout panel in winforms

Time:12-12

I have a UserControl which has 3 labels and 2 pictureboxs. I save database in sql server and has 380 record. Now I have a flow layout panel. I want to load for each record into my User Control. Then I use flow layout panel to add this control. But my application is delayed for doing this. Please help me.

    private void LoadMatch()
    {
        this.Invoke(new Action(() =>
        {
            using (SqlConnection connection = new SqlConnection(@"Data Source=DESKTOP-KBHC686\SQLEXPRESS;Initial Catalog=QLDB;Integrated Security=True"))
            {
                connection.Open();
                string query = "Select T1.PIC,T1.CLBNAME,T2.PIC,T2.CLBNAME,TIME,SCORED1,SCORED2 from CLUB as T1, CLUB as T2, MATCH1 as M where M.CLB1 = T1.IDCLB and "  
                    "M.CLB2 = T2.IDCLB order by DATE asc";

                SqlDataAdapter ada = new SqlDataAdapter(query, connection);
                DataTable dt = new DataTable();
                ada.Fill(dt);

                Match1 match;

                foreach (DataRow row in dt.Rows)
                {
                    match = new Match1();

                    match.lbClubHost.Text = row["CLBNAME"].ToString();
                    match.lbClubVisit.Text = row["CLBNAME1"].ToString();

                    string score1 = row["SCORED1"].ToString();
                    string score2 = row["SCORED2"].ToString();

                    byte[] img = (byte[])row["PIC"];
                    MemoryStream ms = new MemoryStream(img);
                    match.ptbClubHost.Image = Image.FromStream(ms);

                    byte[] img1 = (byte[])row["PIC1"];
                    MemoryStream ms1 = new MemoryStream(img1);
                    match.ptbClubVisit.Image = Image.FromStream(ms1);

                    if (!string.IsNullOrEmpty(score1) && !string.IsNullOrEmpty(score2))
                    {
                        match.lbScore.Text = score1   " - "   score2;
                    }
                    else
                    {
                        match.lbScore.Text = "? - ?";
                    }
                    TimeSpan span = (TimeSpan)row["TIME"];

                    match.lbTime.Text = span.ToString(@"hh\:mm");

                    flpMatch.Controls.Add(match);
                }
                connection.Close();
            }
        }));
    } 

CodePudding user response:

You should use async await for this type of data-loading scenario. This means that the code will be suspended while waiting for the request to return, and the thread can go off dealing with user input.

Using ExecuteReader can be slightly more performant than using a DataAdapter.

You can also bulk-add the controls to the flow-panel using AddRange

private async void LoadMatch()
{
    var list = new List<Match1>();

    try
    {
        const string query = @"
Select
  T1.PIC,
  T1.CLBNAME,
  T2.PIC,
  T2.CLBNAME,
  TIME,
  SCORED1,
  SCORED2
from MATCH1 as M
JOIN CLUB as T1 ON M.CLB1 = T1.IDCLB 
JOIN CLUB as T2 ON M.CLB2 = T2.IDCLB
order by DATE asc;
";
        using (SqlConnection connection = new SqlConnection(@"Data Source=DESKTOP-KBHC686\SQLEXPRESS;Initial Catalog=QLDB;Integrated Security=True"))
        using (var cmd = new SqlCommand(query, connection))
        {
            await connection.OpenAsync();

            using (var reader = await cmd.ExecuteReaderAsync())
            {
                while (await reader.ReadAsync())
                {
                    match = new Match1();

                    match.lbClubHost.Text = reader["CLBNAME"].ToString();
                    match.lbClubVisit.Text = reader["CLBNAME1"].ToString();

                    string score1 = reader["SCORED1"].ToString();
                    string score2 = reader["SCORED2"].ToString();

                    byte[] img = (byte[])reader["PIC"];
                    MemoryStream ms = new MemoryStream(img);
                    match.ptbClubHost.Image = Image.FromStream(ms);

                    byte[] img1 = (byte[])reader["PIC1"];
                    MemoryStream ms1 = new MemoryStream(img1);
                    match.ptbClubVisit.Image = Image.FromStream(ms1);

                    if (!string.IsNullOrEmpty(score1) && !string.IsNullOrEmpty(score2))
                    {
                        match.lbScore.Text = score1   " - "   score2;
                    }
                    else
                    {
                        match.lbScore.Text = "? - ?";
                    }
                    match.lbTime.Text = (TimeSpan)reader["TIME"].ToString(@"hh\:mm");

                    list.Add(match);
                }
            }
        }
        flpMatch.Controls.AddRange(list);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
  • Related