我正在为我的大学开展一个项目,我需要将数据库中的数据绑定到组合框中 . 我需要在组合框的“text”字段中存储roll no / enrollment no,并在组合框的“text”属性中存储学生的名字 .
我的代码是:
#region填充组合框//填充组合框 . public static void FillCombo(ComboBox _cb,string _sSQL,string _sTable){OleDbDataAdapter _oledbDA = new OleDbDataAdapter(_sSQL,_olbedbCN); DataTable _dtSource = new DataTable(); _oledbDA.Fill(_dtSource); _cb.DataSource = _dtSource; _cb.ValueMember = _dtSource.Columns [0] .ColumnName; _cb.DisplayMember = _dtSource.Columns [1] .ColumnName; }
endregion
这里::
_sSQL = "select rollno, studentname from student_data"
我试过的其他代码是:
region填充组合框
//Fill Combo Box.
public static void FillCombo(ComboBox _cb, string _sSQL, string _sTable)
{
OleDbDataAdapter _oledbDA = new OleDbDataAdapter("select rollno, studentname from student_data", _olbedbCN);
DataTable _dtSource = new DataTable();
_oledbDA.Fill(_dtSource);
_cb.DataSource=ds.Tables["StudentData"];
_cb.DisplayMember="Studentname";
_cb.ValueMember="rollno";
_cb.SelectedIndex=0; }
}
endregion
但问题是,组合框中没有加载任何内容....当我运行应用程序时,没有错误,但组合框中没有加载任何内容......
请帮忙......它的SOS ......
1 回答
我更喜欢用从数据库中检索的数据手动填充我的组合框 . 为此,我写了一个我每次都使用的类
MaskedValue
.这是类(从VB.NET转换而来)
要填充组合框,我编写如下代码