Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Trying to get Windows files to load on ASP.Net GridView, but I am getting this DataBinding error message when I tried this code, below. DataBinding: 'System.Web.UI.WebControls.ListItem' does not contain a property with the name....


Can someone please advise? Million Thanks!!

FileGrid.DataBind();   // this line is giving me the problem.


What I have tried:

protected void Page_Load(object sender, EventArgs e) 
{

  if (!IsPostBack)
  {

    string[] filePaths = Directory.GetFiles(Server.MapPath("../Files/"));
    List<listitem> files = new List<listitem>(); 


    foreach (string filePath in filePaths)
    {
        files.Add(new ListItem(Path.GetFileName(filePath), filePath));
        // string fileName = Path.GetFileName(files.ToString());

     }

      FileGrid.DataSource = files;
      FileGrid.DataBind(); // this line is giving me the problem.
  }

}
Posted
Updated 28-Apr-18 7:48am
Comments
Lay_Kay 28-Apr-18 13:44pm    
Never mind.... I discovered where my problem was.

1 solution

This is the syntax (below) to solve the problem after (!IsPostBack).
DataTable dt = new DataTable();

dt.Columns.Add("File");
dt.Columns.Add("Size");
dt.Columns.Add("Type");

foreach (string strfile in Directory.GetFiles(Server.MapPath("../Meow_Directory/")))
{
     FileInfo fi = new FileInfo(strfile);
     dt.Rows.Add(fi.Name, fi.Length.ToString(),
     GetFileType_ByExtension(fi.Extension));
}

FileGrid.DataSource = dt;
FileGrid.DataBind();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900