Fill combobox with database, but start with white

2

I have a combobox that I filled with this database query:

select ' ' as usr_entrada, null as no_servicio union select usr_entrada,  No_Servicio from Telemarketing where Id_Sucursal='cordoba'

The idea of the union is to add a blank row to the selection, so that it appears in the combobox and can be selected. The fact is that the query correctly shows the results, in this way:

+--------------+-------------+
| usr_entrada  | no_servicio |
+--------------+-------------+
|              | NULL        |
+--------------+-------------+
| CAPTURA-TMK  | No_Servicio |
+--------------+-------------+
| SUP          | No_Servicio |
+--------------+-------------+
| TCA02TMK     | No_Servicio |
+--------------+-------------+
| TCACONTABAUX | No_Servicio |
+--------------+-------------+
| TMKCBA01     | No_Servicio |
+--------------+-------------+'

But when filling the combobox, the first line "disappears". The combobox filled it this way:

void llenaUsuarios()
{
     Conexion con = new Conexion();
     DataTable dt=new DataTable();
     using (con.getcon())
     {
          const string sql = "select ' ' as usr_entrada, null as no_servicio union select usr_entrada,  No_Servicio from Telemarketing where [email protected]";
          using(SqlCommand cmd=new SqlCommand(sql, con.getcon()))
          {
               SqlDataReader rd;
               cmd.Parameters.AddWithValue("@Sucursal", cveSucursal);
               rd = cmd.ExecuteReader();
               if (rd.HasRows)
               {
                   rd.Read();
                   dt.Load(rd);
                   comboBox1.DisplayMember = "usr_entrada";
                   comboBox1.ValueMember = "no_servicio";
                   comboBox1.DataSource = dt;
               }
          }
     }
}

Could someone explain to me why this happens? Tengro another combobox that is filled in the same way (although another query), and that does not erase anything.

I thank you in advance for your support.

    
asked by antonio_veneroso 31.07.2017 в 16:58
source

2 answers

1

Although it is not the answer I expected, I found a solution: add the blank row directly to the datatable with this code:

DataRow r = dt.NewRow(); r[0] = ""; r[1] = null; dt.Rows.InsertAt(r, 0);

I put this code after assigning the datatable as the datasource of the combobox. Hopefully someone will serve him in the future.

    
answered by 03.08.2017 / 16:02
source
0

Unfortunately the combo box does not support null values, check this link:

link

However you can add a worthless item to the combo box like this:

comboBox.Items.Insert(0, string.Empty);
    
answered by 31.07.2017 в 17:11