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



我有一个组合框,里面装满了这个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 语句不是问题,我以这种方式填充了另一个组合框,它工作得很好。

谢谢你的时间:-(

所以你想在索引 = 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,您会得到这个想法

在 Windows 窗体组合框、列表框或选中列表框控件中添加和删除项

我上一次研究这些控件是在大约十年前。我正在 SO 中键入一个示例,从我的旧源代码管理存储库中获取参考。

常用变量

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

使用数据表

//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();

使用数据读取器

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编写了它,可能会有一些语法错误!

最新更新