0

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();
1
  • @TimSchmetler I forgot to translate it Commented Sep 26, 2014 at 15:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.