用数据库填充组合框,从空白项开始

时间:2017-07-31 21:40:34

标签: c# sql-server winforms

我有一个填充了这个db语句的组合框:

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

这里的组合框在真实数据之前填充了一个空白项,并且语句完美无缺,这就是结果:

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

问题在于,当我填充组合框时,由于某种原因它会删除空白项目,我不明白为什么。这是我填充组合框的方法:

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 Id_Sucursal=@Sucursal";
          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;
               }
          }
     }
}

谁能告诉我我做错了什么? sql语句不是问题,我有另一个组合框填充,它工作得很好。

感谢您的时间: - )

1 个答案:

答案 0 :(得分:0)

所以你想在index = 0的组合框中插入一个空白项,对吗?

//rest of your code
comboBox1.DataSource = dt;    
comboBox1.Items.Insert(0, new ListItem(" ", "-1")); //After filling the DataSource, insert an item in the Combobox at Index 0

我在这里做的是在数据源填充数据库中的数据后插入一个项目。代码中发生的事情对我来说显而易见。有关这方面的更多信息,请快速阅读。该文章在Windows窗体中显示,但如果您使用的是asp.net,则可以获得该想法

Adding and Removing Items from a Windows Forms ComboBox, ListBox, or CheckedListBox Control

上次我在这些控件上工作的时间差不多是十年前的事了。我在SO中输入一个示例,从旧的Source Control仓库中获取参考。

常见变量

DataRow rw;
public DataSet ds = new DataSet();
public SqlDataReader dr;
public SqlCommand cmd = new SqlCommand();
public SqlDataAdapter adp = new SqlDataAdapter();

使用DataTable

//If DataSet contains the table already, remove it first
if (ds.Tables.Contains(tbl)) 
      ds.Tables.Remove(tbl);

//Open connection here. Better with using
  cmd.CommandText = "YOUR_SQL_QUERY_GOES_HERE";
  adp.SelectCommand = cmd;
  adp.Fill(ds, tbl);
//close the connection here

rw = ds.Tables[tbl].NewRow();        //Add a new row
rw[0] = "-1";                        //Set it's value
rw[1] = "Itemtext";                  //Set it's text
ds.Tables[tbl].Rows.InsertAt(rw, 0); //Insert this row in the DataTable(DT) at Index 0
comboBox1.DataSource = ds.Tables[tbl]; //Assign the DT in the DataSource of the combobox
comboBox1.DataTextField = "Default Text";
comboBox1.DataValueField = "Default Value";
comboBox1.DataBind();

使用DataReader

comboBox1.Items.Clear(); //Clear the dropdown first
//Open the connection, set SqlCommandType and Text
using (SqlDataReader reader = sqlcmd.ExecuteReader())
  {
     comboBox1.DataSource = reader; //Assign DataReader to the DataSource of the Combobox
     comboBox1.DataValueField = "usr_entrada";
     comboBox1.DataTextField = "no_servicio";
     comboBox1.DataBind();
     //Here you can insert a new item at Index 0 of the Combobox.
     //For your case, keep the "Default Text" blank
     comboBox1.Items.Insert(0, new ListItem("--Default Text--", "-1"));
  }
//close the connection

请注意,代码未针对生产使用进行优化,仅为您提供更多创意!所以不要因为代码质量而惩罚答案。我已经直接在SO中编写了它,可能会出现一些语法错误!