未获取组合框选定的值
本文关键字:获取 组合 | 更新日期: 2023-09-27 18:19:52
这是我用来加载组合框的函数。我可以加载组合框,但当我试图获得组合框的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);
我需要组合的代码。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);
填充组合框的代码:
首先,您应该更改代码以填充组合框。它看起来很奇怪。
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);
}
您未能设置数据集。
cmb.DataSource = newDataTable
cmb.ValueMember = valueColumn;
cmb.DisplayMember = textColumn;
return newDataTable;