Remove Null Item from a Combobox c#

Asked

Viewed 190 times

0

Hello, I need to develop an advanced search filter. I created some comboBoxs for the keywords, but whenever I need to fetch the contents of a column that eventually has less content than another particular column, the combobox is filled with Cells in Null.

System in Run. Sistema em Execução.

Brief example of DB. Um breve exemplo do banco de dados.

the 1st Combo I’m loading in Form_load:

        OleDbConnection Con = new OleDbConnection();
        Con.ConnectionString = Properties.Settings.Default.dbConTeste;

        try
        {
            Con.Open();
            OleDbCommand Cmm = new OleDbCommand();
            Cmm.CommandText = "SELECT marca FROM tbmodelo";
            Cmm.CommandType = CommandType.Text;
            Cmm.Connection = Con;
            OleDbDataReader DR;
            DR = Cmm.ExecuteReader();

            DataTable dt = new DataTable();
            dt.Load(DR);

            DataView dv = new DataView(dt, "", "marca", DataViewRowState.OriginalRows);
            comboBox1.DataSource = dv;
            comboBox1.DisplayMember = "marca";
            comboBox1.ValueMember = "";

        }
        catch 
        {
            MessageBox.Show("error");
        }
        Con.Close();

the 2nd Combo I load in the Combobox1_leave event:

        OleDbConnection Con1 = new OleDbConnection();
        Con1.ConnectionString = Properties.Settings.Default.dbConTeste;

        try
        {
            Con1.Open();
            OleDbCommand Cmm = new OleDbCommand();
            Cmm.CommandText = "SELECT " + comboBox1.Text + " FROM tbmodelo";
            Cmm.CommandType = CommandType.Text;
            Cmm.Connection = Con1;
            OleDbDataReader DR;
            DR = Cmm.ExecuteReader();

            DataTable dt = new DataTable();
            dt.Load(DR);


            DataView dv = new DataView(dt, "", comboBox1.Text, DataViewRowState.OriginalRows);
            comboBox2.DataSource = dv;
            comboBox2.DisplayMember = comboBox1.Text;
            comboBox2.ValueMember = "";
        }
        catch
        {
            MessageBox.Show("error");
        }
        Con1.Close();

Does anyone indicate a simpler way to load Combos without the empty fields? or just delete the fields in Null. ?

1 answer

1


OleDbConnection Con = new OleDbConnection();
    Con.ConnectionString = Properties.Settings.Default.dbConTeste;

    try
    {
        Con.Open();
        OleDbCommand Cmm = new OleDbCommand();
        Cmm.CommandText = "SELECT marca FROM tbmodelo where MARCA <> ''";
        Cmm.CommandType = CommandType.Text;
        Cmm.Connection = Con;
        OleDbDataReader DR;
        DR = Cmm.ExecuteReader();

        DataTable dt = new DataTable();
        dt.Load(DR);

        DataView dv = new DataView(dt, "", "marca", DataViewRowState.OriginalRows);
        comboBox1.DataSource = dv;
        comboBox1.DisplayMember = "marca";
        comboBox1.ValueMember = "";

    }
    catch 
    {
        MessageBox.Show("error");
    }
    Con.Close()

OleDbConnection Con1 = new OleDbConnection();
    Con1.ConnectionString = Properties.Settings.Default.dbConTeste;

    try
    {
        Con1.Open();
        OleDbCommand Cmm = new OleDbCommand();
        Cmm.CommandText = "SELECT " + comboBox1.Text + " FROM tbmodelo where " + comboBox1.Text + " <> '';
        Cmm.CommandType = CommandType.Text;
        Cmm.Connection = Con1;
        OleDbDataReader DR;
        DR = Cmm.ExecuteReader();

        DataTable dt = new DataTable();
        dt.Load(DR);


        DataView dv = new DataView(dt, "", comboBox1.Text, DataViewRowState.OriginalRows);
        comboBox2.DataSource = dv;
        comboBox2.DisplayMember = comboBox1.Text;
        comboBox2.ValueMember = "";
    }
    catch
    {
        MessageBox.Show("error");
    }
    Con1.Close();
  • 1

    Just for the record that if the field value is NULL (and not empty), will not work.

Browser other questions tagged

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