Remove Null Item from a ComboBox c #

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 column, the comboBox is populated with Cells in Null.

Run System.

BriefexampleofDB.

1stComboI'mloadinginForm_Load:

OleDbConnectionCon=newOleDbConnection();Con.ConnectionString=Properties.Settings.Default.dbConTeste;try{Con.Open();OleDbCommandCmm=newOleDbCommand();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();

2nd Combo loads in 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 else indicate a simpler way to load Combos without the blank fields? or just delete the fields in Null. ?

    
asked by anonymous 29.07.2016 / 20:51

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();
    
29.07.2016 / 21:16