4

我想在 .net 3.5 中使用一些分隔符将我的下拉列表与两个值绑定 [如 '-' 或 '/']。

 qry = "SELECT CityName, MasterState.StateName FROM MasterCity INNER JOIN "+
              "MasterState ON MasterCity.StateID = MasterState.StateID order by CityName";

DropCity.DataMember = "DeafultView";
//DropCity.DataTextField = "CityName";
//DropCity.DataValueField = "CityName";
DropCity.DataTextFormatString = "{0} - {1}";
DropCity.DataTextField = "CityName,StateName";
DropCity.DataSource = dt;
DropCity.DataBind();
DropCity.Items.Insert(0, "-Select-");**

我做到了,但它给出了错误

DataBinding:“System.Data.DataRowView”不包含名为“CityName,StateName”的属性。”}

我怎样才能做到这一点?

4

3 回答 3

3
DropCity.DataTextField = "CityName,StateName";

DataTextField将寻找列CityName,StateName,但它不存在。

也许您可以像这样更改您的查询

qry = "SELECT CityName, CityName + '-' + MasterState.StateName as CityState
    FROM MasterCity INNER JOIN "+
          "MasterState ON MasterCity.StateID = MasterState.StateID order by CityName";

    DropCity.DataTextField = "CityState"; 
于 2013-02-13T08:44:20.977 回答
0

不要认为一次绑定两个东西是可能的,尽管你可以改变你的 SQL 查询来返回你需要的东西。

改变:-

qry = "SELECT CityName, MasterState.StateName FROM MasterCity INNER JOIN "+
  "MasterState ON MasterCity.StateID = MasterState.StateID order by CityName";

qry = "SELECT CityName, MasterState.StateName, " + 
     "CityName + ' - ' + MasterState.StateName AS DDName " +
      " FROM MasterCity INNER JOIN "+
      "MasterState ON MasterCity.StateID = MasterState.StateID order by CityName";

然后改变: -

DropCity.DataTextField = "CityName,StateName";

到 :-

DropCity.DataTextField = "DDName";
于 2013-02-13T08:44:48.033 回答
0

试试这个..我稍微修改了查询。希望它有效。

qry = "SELECT CityName , CityName + "-" + MasterState.StateName as CityNState FROM
        MasterCity INNER JOIN MasterState ON MasterCity.StateID = MasterState.StateID order by
        CityName";

DropCity.DataMember = "DeafultView";
DropCity.DataTextField = "CityNState";
DropCity.DataSource = dt;
DropCity.DataBind();
DropCity.Items.Insert(0, "-Select-");
于 2013-02-13T08:55:05.150 回答