Combobox slowing form loading winforms C# visual studio data

Asked

Viewed 343 times

1

Good evening, I have a question. I have a customer register that lists cities in a combobox, only every time I open the register it loads the data and this slows the loading of the form, there is some way to do the query only the first time you open the form and then reuse?

This is the code that populates the checkbox

       String nomeConexao = LoginInfo.StringConexao;
       String string_conn = 
       ConfigurationManager.ConnectionStrings[nomeConexao].ConnectionString;
        SqlConnection conn = new SqlConnection(string_conn);
        try
        {
            conn.Open();
        }
        catch (SqlException sqle)
        {
            MessageBox.Show("Falha ao efetuar a conexão. Erro: " + sqle);
        }
        String CodCidadeEmpresa = DadosEmpresa.CodCidade;

        String sql = "SELECT COD, CIDADE FROM CODMUNICIPIO ORDER BY CIDADE";
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        DataTable dtResultado = new DataTable();
        dtResultado.Clear();
        CbCidade.DataSource = null;
        da.Fill(dtResultado);
        CbCidade.DataSource = dtResultado;
        CbCidade.ValueMember = "COD";
        CbCidade.DisplayMember = "CIDADE";
        CbCidade.SelectedItem = "";
        CbCidade.SelectedValue = CodCidadeEmpresa;
        CbCidade.Refresh();
        conn.Close();
  • blz, Thank you so much

2 answers

1


Another option would be to load the Dropdown asynchronously, so it wouldn’t stop the form while it is loaded:

Create an asynchronous method as follows:

private async void CarregaDropDownCidades()
{
    String nomeConexao = LoginInfo.StringConexao;
    tring string_conn = 
    onfigurationManager.ConnectionStrings[nomeConexao].ConnectionString;
    SqlConnection conn = new SqlConnection(string_conn);
    try
    {
        conn.Open();
    }
    catch (SqlException sqle)
    {
        MessageBox.Show("Falha ao efetuar a conexão. Erro: " + sqle);
    }
    String CodCidadeEmpresa = DadosEmpresa.CodCidade;

    String sql = "SELECT COD, CIDADE FROM CODMUNICIPIO ORDER BY CIDADE";
    SqlDataAdapter da = new SqlDataAdapter(sql, conn);
    DataTable dtResultado = new DataTable();
    dtResultado.Clear();    
    da.Fill(dtResultado);

    Invoke((MethodInvoker)delegate
    {
        CbCidade.DataSource = null;
        CbCidade.DataSource = dtResultado;
        CbCidade.ValueMember = "COD";
        CbCidade.DisplayMember = "CIDADE";
        CbCidade.SelectedItem = "";
        CbCidade.SelectedValue = CodCidadeEmpresa;
        CbCidade.Refresh();
    }); 
    conn.Close();
}

In the load in your own way make the following call:

private async void Form1_Load(object sender, EventArgs e)
{
    await Task.Run(() =>
    {
        CarregaDropDownCidades();
    });
}
  • Cara worked very well, he only gives a light locked in the open but nothing that compares to what it was. Thank you. Hug

  • @Rafa No more items loading database data? you can do this for all components that are "weighing" the load aching form

  • I’ll give you an organized in my code, surely there must be something.

0

Try to remove the ORDER BY CIDADE from your select and sort directly in the combo CbCidade.Sorted = true;.

If the above hint does not solve, then try the second method

Create a city class

public class Cidades
{
  public int id {get; set;}
  public string nome {get; set;}
}

Then in the main form create a published list

public List<Cidades> ComboCidades {get; set;}

And then on startup of your system you load it with select data

So when you open your form that has the city combo you fill it with the list entries +/- like this

CbCidade.DataSource = ComboCidades;
CbCidade.DisplayMember = "nome";
CbCidade.ValueMember = "id";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.