【发布时间】:2017-10-02 19:31:37
【问题描述】:
这是给出错误的代码
DataSet Music = new DataSet();
Music= DBI.musicall();
comboBox1.DisplayMember= "music1.name"; // with .
comboBox1.ValueMember="Id";
comboBox1.DataSource=Music.Tables[0];
这是正在运行的代码。
DataSet Music = new DataSet();
Music= DBI.musicall();
comboBox1.DisplayMember= "music1name"; // no point -- no .
comboBox1.ValueMember="Id";
comboBox1.DataSource=Music.Tables[0];
public static DataSet musicall()
{
SqlConnection connect= new SqlConnection(connectway);
string sql = "select *from Music";
SqlCommand command= new SqlCommand();
command.CommandText=sql;
command.Connection=connect;
SqlDataAdapter adaptor = new SqlDataAdapter();
adaptor.SelectCommand=command;
DataSet finalDs= new DataSet();
connect.Open();
adaptor.Fill(finalDs);
connect.Close();
return finalDs;
}
为什么我在列名中使用point(.)时会遇到这个问题。
为什么当我在列名中使用“.”时,组合框会显示该错误?
我可以不使用“.”来解决这个问题,但我不想那样解决这个问题。
【问题讨论】:
-
musicall();的代码在哪里?? -
什么是音乐的
CREATE TABLE? -
顺便说一句,你不应该将你的字段命名为
music.name该字段已经在music表中,所以是多余的。 -
它可能不受 .NET 数据绑定逻辑的支持,因为
DisplayMember需要在绑定组合框时在数据源中查找的属性名称。并且根据标准规则属性中不能有点 (.)。这会导致无法找到财产及其价值。这就是它显示标准字符串表示的原因。如果您检查组合框的 SelectedValue 属性,您应该得到正确的值,因为“Id”是一个有效的属性名称。 -
@Australopithecus,您是否可以考虑 (而不是使用 *) 如下声明表的每一列?
select music.name AS [MusicName] FROM Music