这是我用来加载组合框的功能。我可以加载组合框,但当我尝试获得selectedvalue
组合框时,它会显示null
;我没有得到实际价值。
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);
填充组合框的代码:
答案 0 :(得分:0)
您错过了设置数据集。
cmb.DataSource = newDataTable
cmb.ValueMember = valueColumn;
cmb.DisplayMember = textColumn;
return newDataTable;
答案 1 :(得分: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);
}