C# DropDownList 中的 System.Data.DataRowView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15522025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.Data.DataRowView in DropDownList
提问by whoadityanawandar
I am trying to populate a dropdown using values from a column. Now the problem is: I am not getting the actual values (the country codes like India(+61)) in the dropdown. Instead I am getting "System.Data.DataRowView" (multiple times) in the dropdown.
我正在尝试使用列中的值填充下拉列表。现在的问题是:我没有在下拉列表中获得实际值(像印度(+61)这样的国家/地区代码)。相反,我在下拉列表中得到“System.Data.DataRowView”(多次)。
public void bind()
{
DataSet ds1 = new DataSet();
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
con.Open();
string strQuery = "select CountryCode from AUser";
SqlCommand cmd = new SqlCommand(strQuery, con);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
da.Fill(ds1, "AUser");
ddlMobile.DataSource = ds1.Tables["AUser"];
ddlMobile.DataBind();
con.Close();
}
I am calling the bind method on page_load
. Data type for CountryCode
is varchar(50)
& values are like India(+91)
, Australia(+61)
etc...
我正在调用 bind 方法page_load
。数据类型CountryCode
是varchar(50)
和值是一样India(+91)
,Australia(+61)
等...
采纳答案by nunespascal
You should set the DataValueField
and DataTextField
Properties of the drop down.
您应该设置下拉菜单的DataValueField
和DataTextField
属性。
ddlMobile.DataSource = ds1.Tables["AUser"];
ddlMobile.DataValueField = "CountryCode";
ddlMobile.DataTextField = "CountryName";
ddlMobile.DataBind();
Here CountryCode and CountryName must be the column names corresponding to those values in your DataRow
这里 CountryCode 和 CountryName 必须是与 DataRow 中这些值对应的列名
回答by Richard
You are seeing what the default implementation of DataRowView.ToString() does. To pick specific fields from within the DataRow to display, do something like this.
您将看到 DataRowView.ToString() 的默认实现做了什么。要从 DataRow 中选择要显示的特定字段,请执行以下操作。
ddlMobile.DataSource = ds1.Tables["AllUser"];
ddlMobile.DataTextField = "CountryCode"; // This is text displayed
ddlMobile.DataValueField = "CountryCode"; // This is the value returned
ddlMobile.DataBind();
回答by Agustin Meriles
You haven't set the DataTextField
in the DropDownList
. It's recommended to set also the DataValueField
In your aspx add the DataTextField
property:
你还没有DataTextField
在DropDownList
. 建议还设置DataValueField
在您的 aspx 添加DataTextField
属性:
<asp:DropDownList ID="ddlMobile" runat="server"
DataTextField="CountryCode"
DataValueField="CountryCode" />
You can also set it in the code behind, like the other answers show.
您也可以在后面的代码中设置它,就像其他答案显示的那样。
Otherwise the behaviour that you are seeing, is because the DataBound is calling the ToString()
to display the info, as you don't provided wich data field look for.
否则,您看到的行为是因为 DataBound 正在调用ToString()
以显示信息,因为您没有提供查找的数据字段。