当我得到SelectedItem在组合框返回System.Data.DataRowView
本文关键字:返回 System Data DataRowView 组合 SelectedItem | 更新日期: 2023-09-27 18:05:13
这是一个在SQL中检索两个字段到组合框的函数:代码:
public void FillCmbKala()
{
cmbKala.Items.Clear();
objCon.Connecting();
string SQL = "SELECT [kID],[kName] FROM tblKala ORDER BY kName";
DataSet ds = new DataSet();
using (SqlConnection cn = new SqlConnection(objCon.StrCon))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(SQL, cn))
{
cn.Open();
try
{
adapter.Fill(ds);
}
catch (SqlException e)
{
MessageBox.Show("There was an error accessing your data of 'Kala'. DETAIL: " + e.ToString());
}
finally
{
cn.Close();
}
}
}
cmbKala.DataSource = ds.Tables[0];
cmbKala.DisplayMember = "kName";
cmbKala.ValueMember = "kID";
}
当我使用组合框返回System.Data.DataRowView !
例如:
string str= cmbKala.SelectedItem;
result is: str= System.Data.DataRowView
MSDN参考
作为SelectedItem
返回对象。它返回选定的特定行对象。下面是获取value的方法:
DataRowView oDataRowView = cmbKala.SelectedItem as DataRowView;
string sValue = string.Empty;
if (oDataRowView != null) {
sValue = oDataRowView.Row["kName"] as string;
}
DataRowView dv = (DataRowView)comboBox1.SelectedItem;
string s = (string)dv.Row["kName"];
int m1 = (int)dv.Row["kID"];
你选择的是一个DataRowView。你应该选择项目的id或者文本,对吗?如字符串str= cmbkala . selecteitem . text或字符串str= cmbkala . selecteitem . value ?
我知道这个问题很老了,但我只是想让你们知道,如果你得到这个错误,确保你正在使用
ComboBoxName.DisplayMemberPath = "name of the column that you want to show in the combobox (ex: name)"
ComboBoxName.SelectedValuePath = "name of the column (ex:id)";
这肯定对你有帮助
在你想加载的事件上,只写下面的代码
onformload()
{
cmb_dept.Items.Clear();
SqlConnection conn = new SqlConnection(@"DATA SOURCE=(localdb)'MSSQLLocalDB;INTEGRATED SECURITY=true;INITIAL CATALOG=EMPLOYEE;");
conn.Open();
SqlCommand command = new SqlCommand("select dept_id, dept_name from department", conn);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);
cmb_dept.ValueMember = "dept_id";
cmb_dept.DisplayMember = "dept_name";
cmb_dept.DataSource = ds.Tables[0];
}
使用您想要访问值的代码........string dept = cmb_dept.Text; MessageBox.Show("val=" + dept);
YOUR combobox.text = System.Data.DataRowView Will be Solved ##
这适用于c# WPF:
Textbox.Text = (listbox.SelectedItem as DataRowView).Row["Purpose"] as string;