System.Data.DataRowView,c#
我有个问题:System.Data.DataRowView,c#,c#,visual-studio,C#,Visual Studio,我有个问题: SqlDataAdapter da = new SqlDataAdapter("SELECT dbo.User.name FROM dbo.User", con); DataTable dt = new DataTable(); comboBox1.DataSource = dt; da.Fill(dt); 它在每一行返回System.Data.DataRowView 我试过: comboBox1.ValueMember = "idUser"; comboBox1.Display
SqlDataAdapter da = new SqlDataAdapter("SELECT dbo.User.name FROM dbo.User", con);
DataTable dt = new DataTable();
comboBox1.DataSource = dt;
da.Fill(dt);
它在每一行返回System.Data.DataRowView
我试过:
comboBox1.ValueMember = "idUser";
comboBox1.DisplayMember = "dbo.User.name"
我得到一个错误:
无法绑定到新的显示成员。参数名称:
新显示成员
我想获得idUser
和name
。
谢谢填写完数据表后,您需要在SELECT语句中返回ID,并将数据表指定为数据源:
SqlDataAdapter da = new SqlDataAdapter("SELECT IDUser, Name FROM dbo.User", con);
DataTable dt = new DataTable();
da.Fill(dt);
然后可以将组合框设置为:
comboBox1.DataSource = dt.DefaultView;
comboBox1.ValueMember = "IDUser";
comboBox1.DisplayMember = "Name"
更新
如果要访问组合框中选定项目的选定文本或值,则需要执行以下操作:
DataRowView drvItem = comboBox1.SelectedItem as DataRowView;
if (drvItem != null)
{
label4.Text = drvItem["ID"].ToString();
label3.Text = drvItem["Name"].ToString();
}
请在下面使用
SqlDataAdapter da = new SqlDataAdapter("SELECT IDUser, Name FROM dbo.User", con);
DataTable dt = new DataTable();
da.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.ValueMember = "IDUser";
comboBox1.DisplayMember = "Name";
谢谢,现在我得到了一排idUser。但是我仍然无法在secelct上显示:private void comboBox1_SelectedIndexChanged(对象发送方,事件参数e){object personVal=comboBox1.DisplayMember;label3.Text=Convert.ToString(personVal);object personSel=comboBox1.ValueMember;label4.Text=Convert.ToString(personSel)}@janolmayti我已更新了答案,以显示如何访问所选项目数据。