2017-07-31 177 views
1

我有一個充滿這個分貝語句組合框:填充組合框與數據庫,從空白項目

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 [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; 
       } 
      } 
    } 
} 

誰能告訴我我做錯了什麼? sql語句不是問題,我有另一個組合框填充,它工作得很好。

謝謝您的時間:-)

+0

您正在將comboBox1的DataSource設置爲DataTable,該DataTable不包含空格。在從數據庫讀取數據之前,您不應該將靜態數據添加到DataTable中嗎? –

+0

謝謝Robert的評論,空白項目被添加到sql語句中。 –

回答

0

所以,你想在指數= 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

上次我和這些控件的工作幾乎是在十年前。我在這裏輸入了一個樣本,從我以前的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,可能會有一些語法錯誤!

+0

謝謝你的回答!它工作正常,但我真的很想知道我的代碼中出了什麼問題,因爲我有另外兩個組合框像這樣填充(當然有另一個聲明),這兩個工作正常。 –

+0

這很難說!你是否設置了其他兩個組合框的屬性以擁有默認項目?選擇其中任何一個,按F4並檢查屬性窗口?如果你找不到任何東西,試試這個 - 複製一個工作組合框,然後嘗試。如果這有效,那麼肯定有一些屬性分配給它。 – sandiejat