Fill several combobox

Asked

Viewed 917 times

3

I can fill a combobox with Datatable, but in my case I need to fill 2 combobox and 1 checkedboxlist with data from a Mysql comic

//formulário cadastro
private void frmCadastroRecibo_Load(object sender, EventArgs e)
{
    Classes.DBconect con = new Classes.DBconect();
    string sqlc = "SELECT * FROM cliente";
    DataTable dtc = new DataTable();
    MySqlDataReader drc = con.returnDataR(sqlc);
    dtc.Load(drc);
    cmbCliente.DisplayMember = "nome";
    cmbCliente.ValueMember = "id";
    cmbCliente.DataSource = dtc;
}

//classe de ligação aplicação e bd
public MySqlDataReader returnDataR(string querySql)
{
    str_sql = querySql;
    MySqlConnection exec = new MySqlConnection();
    exec = abrirBanco();
    MySqlCommand comando = new MySqlCommand();
    comando.CommandText = str_sql.ToString();
    comando.Connection = exec;
    MySqlDataReader sqlReader = comando.ExecuteReader();
    return sqlReader;
}

How can I do that?

  • I usually make a generic method for this. Then I pass in the argument of the method a parameter of the type of combobox and also of the checklistbox. It can be an overcharged method. One for combo and one for Checklist.

  • unfortunately I don’t know how to do this.

  • 1

    you need to fill the combobox s and the checkedboxlist with the same data?

  • 1

    Just replicate the solution you already have for another ComboBox and to the CheckedListBox, since the allocation of the DataSource is exactly the same!

2 answers

0

You can do the same type of procedure using a MySqlDataAdapter to fill your DataTable. Once you have the table populated with the data only use the properties of the ComboBox and of CheckListBox:

  • SuaComboBox.DataSource = sua Tabela;
  • SuaComBox.ValueMember = "O campo que você que o rertono";
  • SuaComBox.DisplayMember = "O Campo do item que você quer q seja exibido";

The Control CheckListBox has the same properties.

An illustration of the use of MySqlDataAdapter: inserir a descrição da imagem aqui

In this Illustration create a Load Name Method that is assigned to the event .Load, after the initialization of the Form Components, executing the queries and fed the controls.

0

A simple example of how to do it. Then you adapt according to your need.

public bool preencheTipo_Usuario(DropDownList dl)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("select ");
            sb.AppendLine("codtipo, descricao_tipo");
            sb.AppendLine("from tbl_tipo_usuario ");

            SqlConnection conexao = new SqlConnection();
            conexao.ConnectionString = ConfigurationManager.ConnectionStrings["conectDarf"].ConnectionString;
            this.cmd = new SqlCommand(sb.ToString(), conexao);

            try
            {
                conexao.Open();
                cmd.ExecuteNonQuery();

                SqlDataReader dr = cmd.ExecuteReader();

                dl.DataSource = dr;
                dl.DataTextField = "descricao_tipo";
                dl.DataValueField = "codtipo";
                dl.DataBind();

                dl.Items.Insert(0, new ListItem("--- SELECIONE ---", "-1"));
            }
            catch (Exception excecao)
            {
                Erro = excecao.Message;
                return false;
            }
            finally
            {
                conexao.Close();

            }

            return true;
        }
  • Because you use cmd.ExecuteNonQuery(); after opening the connection and then use cmd.ExecuteReader();?

  • DropDownList? It intends to load the data a ComboBox and A CheckListBox. The Properties DataTextField and DataValueField are not part of Control ComboBox in namespace System.WindowsForm.

Browser other questions tagged

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