gpt4 book ai didi

c# - System.Data.DataRowView,C#

转载 作者:行者123 更新时间:2023-11-30 19:46:18 25 4
gpt4 key购买 nike

我有一个问题:

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"

然后出现错误:

Cannot bind to the new display member. Parameter name: newDisplayMember

我想获取idUsername。谢谢

最佳答案

填写DataTable后,需要在SELECT语句中返回ID,并将DataTable指定为DataSource:

 SqlDataAdapter da = new SqlDataAdapter("SELECT IDUser, Name FROM dbo.User", con);
DataTable dt = new DataTable();
da.Fill(dt);

然后您可以这样设置 ComboBox:

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();
}

关于c# - System.Data.DataRowView,C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8786637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com