Reputation: 9928
When I call the following method as a datasource of a dropdownlist, I get System.Data.DataRowView instead of folder names. Where am I doing wrong?
public DataTable listFolders()
{
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("fullname", typeof(string));
string defaultPath = Server.MapPath(ConfigurationManager.AppSettings["defaultPath"].ToString());
foreach (var dir in new DirectoryInfo(defaultPath).GetDirectories("*", SearchOption.TopDirectoryOnly))
{
dr = dt.NewRow();
dr["name"] = dir.Name;
dr["fullname"] = dir.FullName;
dt.Rows.Add(dr);
}
return dt;
}
My method call
ddl.DataSource = listFolders();
ddl.DataBind();
Upvotes: 0
Views: 125
Reputation: 460208
You have to specify the DataTextField
and DataValueField
:
ddl.DataSource = listFolders();
ddl.DataTextField = "name"; // or fullname
ddl.DataValueField = "fullname"; // or name
ddl.DataBind();
Otherwise .NET doesn't know which field you want to show and which you want to use as key. You can also use only one of both, then the text is also the value and vice-versa. But you cannot omit it, otherwise object.ToString()
is used which is the full-typename in case of a DataRowView
.
Upvotes: 1