System.Data.DataRowView,C# [英] System.Data.DataRowView, c#
本文介绍了System.Data.DataRowView,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题:
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"
和我得到错误:
无法绑定到新的显示成员。参数名:
newDisplayMember
Cannot bind to the new display member. Parameter name: newDisplayMember
我想获得 ID用户
和名称
。
谢谢
I want to get idUser
and name
.
Thanks
推荐答案
您需要在您的SELECT语句返回ID和分配数据表的DataSource,你可以以后漫天的数据表:
You need to return the ID in your SELECT statement and assign the DataTable as the DataSource after you have filled the DataTable:
SqlDataAdapter da = new SqlDataAdapter("SELECT IDUser, Name FROM dbo.User", con);
DataTable dt = new DataTable();
da.Fill(dt);
您可以然后设置组合框为这样的:
You can then set up the ComboBox as such:
comboBox1.DataSource = dt.DefaultView;
comboBox1.ValueMember = "IDUser";
comboBox1.DisplayMember = "Name"
更新
如果您要访问ComboBox中选定的项目选定的文本或值,那么你需要做的是这样的:
If you want to access the selected text or value of the item selected in the ComboBox, then you will need to do something like:
DataRowView drvItem = comboBox1.SelectedItem as DataRowView;
if (drvItem != null)
{
label4.Text = drvItem["ID"].ToString();
label3.Text = drvItem["Name"].ToString();
}
这篇关于System.Data.DataRowView,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文