2014-08-29 69 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