2014-08-29 71 views
0

這是我用來加載組合框的函數。我可以加載組合框,但是當我嘗試獲取組合框selectedvalue時,它顯示null;我沒有得到實際的價值。Combobox選擇的值沒有得到

public static DataTable GetComboBoxedDataTable(DataTable oldDataTable, string valueColumn, string textColumn, string topRowValue, string topRowText, ComboBox cmb) 
{ 
    DataTable newDataTable = new DataTable(); 
    newDataTable.Columns.Add(valueColumn); 
    newDataTable.Columns.Add(textColumn); 
    foreach (DataRow oldDR in oldDataTable.Rows) 
    { 
     DataRow newDR = newDataTable.NewRow(); 
     newDR[0] = oldDR[valueColumn].ToString(); 
     newDR[1] = oldDR[textColumn].ToString(); 
     newDataTable.Rows.InsertAt(newDR, newDataTable.Rows.Count); 
    } 

    // Add your 'Select an item' option at the top 
    DataRow dr = newDataTable.NewRow(); 
    dr[0] = topRowValue; 
    dr[1] = topRowText; 
    newDataTable.Rows.InsertAt(dr, 0); 

    cmb.ValueMember = valueColumn; 
    cmb.DisplayMember = textColumn; 
    return newDataTable; 
} 

的代碼來填充組合框:

PolosysHMS.General.Classes.GeneralClass.GetComboBoxedDataTable(ds.Tables[0], "RoomID", "RoomNo", "0", "Select", cmbroomno); 

這裏我需要combobox.selectedvalue的代碼:

private void cmbroomno_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      try 
      { 
       object[,] ParamArray = new object[,] { { "@RoomID", cmbroomno.SelectedValue } };---code where i need selected value 
       DataSet ds = new DataSet(); 
       ds = DB.ExecuteQuery_SP("SelectGuestDetailsForService", ParamArray); 

的代碼來填充組合框:

+1

第二件事告訴你如何獲得所選擇的值的代碼。 – Mairaj 2014-08-29 04:58:15

+1

你在哪裏設置了DataSource for combo? – Seminda 2014-08-29 04:58:59

+0

// DataSet ds = DB.ExecuteQuery_SP(「SelectRoomsByCheckStatus」); //PolosysHMS.General.Classes.GeneralClass.GetComboBoxedDataTable(ds.Tables[0],「RoomID」,「RoomNo」,「0」,「Select」,cmbroomno); – 2014-08-29 05:07:02

回答

0

首先,你應該改變你的代碼來填充組合框。它看起來很奇怪。

public static DataTable GetComboBoxedDataTable(DataTable oldDataTable, string valueColumn, string textColumn, string topRowValue, string topRowText, ComboBox cmb) 
{ 
    DataTable newDataTable = oldDataTable.Copy(); 
    DataRow dr = newDataTable.NewRow(); 
    dr[0] = topRowValue; 
    dr[1] = topRowText; 
    newDataTable.Rows.InsertAt(dr, 0); 

    cmb.ValueMember = valueColumn; 
    cmb.DisplayMember = textColumn; 
    cmb.DataSource = newDataTable; 
    return newDataTable; 
} 

您聲明一個多維數組存儲值

private void cmbroomno_SelectedValueChanged(object sender, EventArgs e) 
{ 
    object[,] ParamArray = new object[,] { { "@RoomID", cmbroomno.SelectedValue } }; 
    DataSet ds = DB.ExecuteQuery_SP("SelectGuestDetailsForService", ParamArray); 
} 
+0

shell我更新我的答案 – 2014-08-29 11:22:34

0

你錯過了設置DataSet。

cmb.DataSource = newDataTable 
cmb.ValueMember = valueColumn; 
cmb.DisplayMember = textColumn; 
return newDataTable; 
+0

綁定數據源數據表就足夠了......如果我們使用數據集,我們正在選擇一個表...... – 2014-08-29 05:19:49

+0

你說你沒有獲得選定項目的價值?對。因爲你的cmb數據源爲空。只需用我的代碼替換代碼並檢查。 – 2014-08-29 05:23:57

+0

Combobox綁定並顯示組合框中的數據,但是如果數據源爲null,則顯示空值如果組合框將綁定並顯示數據 – 2014-08-29 05:25:56