List items in addition to the database in Combobox in c# winforms

Asked

Viewed 70 times

0

I have an application in C# that performs a search. For this is used a field called 'status' that is no longer a list in a combobox. I need it to appear beyond the status that comes from the bank, a status called 'ALL' that does not exist in the bank, and if possible that it gets first on the combobox when listing. Follow my code to list the fields.

String string_conn = ConfigurationManager.ConnectionStrings["conexao"].ConnectionString;
NpgsqlConnection conn = new NpgsqlConnection(string_conn);
      try
          {
            conn.Open();
           }
           catch (NpgsqlException sqle)
          {
              MessageBox.Show("Falha ao efetuar a conexão. Erro: " + sqle);
           }
                String sql = "SELECT * FROM usuario";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);
                DataTable dtResultado = new DataTable();
                dtResultado.Clear();
                da.Fill(dtResultado);

                Invoke((MethodInvoker)delegate
                {
                    CbUsuario.DataSource = null;
                    CbUsuario.DataSource = dtResultado;
                    CbUsuario.ValueMember = "cod";
                    CbUsuario.DisplayMember = "nome";
                    CbUsuario.SelectedItem = "";
                    CbUsuario.Refresh();
                });
                conn.Close();
  • in the CbUsuario you want to add the status "all"?

  • first, show how you are filling the Status combo... then take a look at this answer I just put, you can greatly improve your code: https://answall.com/a/311774/69359 (in the example you have Oledb, but you have all the respective classes for Npgsql)

  • this, is so that when I go to make a query selecting ALL it brings all the results, on the contrary it brings only what this selected.

1 answer

2


Based on that reply of the OS is not possible add directly a new item in the combobox that is filled through the Datasource, to do this you will need to add the item to the object being made the Binding, in your case, add in DataTable, your code would look something like this:

String string_conn = ConfigurationManager.ConnectionStrings["conexao"].ConnectionString;
NpgsqlConnection conn = new NpgsqlConnection(string_conn);
try
{
  conn.Open();
}
catch (NpgsqlException sqle)
{
    MessageBox.Show("Falha ao efetuar a conexão. Erro: " + sqle);
}
String sql = "SELECT * FROM usuario";
NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);
DataTable dtResultado = new DataTable();
dtResultado.Clear();
da.Fill(dtResultado);

//Adiciona uma linha com código 0 e texto "todos"
dtResultado.Rows.Add("0", "TODOS");

//Ordena por id o datatable
DataView view = dtResultado.DefaultView;
view.Sort = "cod ASC";
DataTable dtSorted = view.ToTable();

Invoke((MethodInvoker)delegate
{
    CbUsuario.DataSource = null;
    CbUsuario.DataSource = dtResultado;
    CbUsuario.ValueMember = "cod";
    CbUsuario.DisplayMember = "nome";
    CbUsuario.SelectedItem = "";
    CbUsuario.Refresh();
});
conn.Close();

In order for the item "ALL" to appear first, a sort has been done by column Cod of DataTable

  • 1

    Perfect! It was what I needed. Thanks Master.

Browser other questions tagged

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