這是我用來加載組合框的函數。我可以加載組合框,但是當我嘗試獲取組合框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);
的代碼來填充組合框:
第二件事告訴你如何獲得所選擇的值的代碼。 – Mairaj 2014-08-29 04:58:15
你在哪裏設置了DataSource for combo? – Seminda 2014-08-29 04:58:59
// 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