简体   繁体   中英

Delete the uploaded Excel file after bind the data source to gridview in c#.net

I want to delete the excel and csv file after complete reading and bind data source to the grid view.. However after I successful deleted the file, my gridview has empty data...

DataTable dt = GetCsvData(_path, _filename);
gvList.DataSource = dt;
gvList.DataBind();      // before delete, gridview has data..
File.Delete(_path+_filename);    // after delete, gridview has empty data

Deleting the file could not have any relation to the fact that your grid view is empty. Obviously the call to GetCsvData loads the entire file into a DataTable so there must be no harm on deleting the file.

Debug the code, and make sure after the call to GetCsvData the dt contains data.

If dt contains data, you are probably performing data binding in the wrong time. Put the code in Page_Load . If you do the binding in a later phase in the page life cycle, probably the data will not be rendered.

gvList.DataSource = dt.Copy();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM