开发者

System.OutOfMemoryException when binding a gridview to a datatable having more then 400000 records

Dear, I am facing system.outofmemmory exception when binding a gridview with a datatable having more then 400000 records please find the below sample code for the same

GridView gv = new GridView();
this.EnableViewState = false;
gv.DataSource = (DataTable)dt;
gv.DataBind();

Kindly help me to overcome in this situation is there any limitation of gridview for databind开发者_JAVA技巧?


A datagrid is limited by available system memory, and you're likely running into that. Assuming a process address space of, say, 1.5GB, that's about 4KB per datagrid row, if you had nothing else consuming memory in your process. 4KB is not unreasonable, assuming a dozen or so wide columns.

You're trying to display too many rows at once. I have to assume it's a pretty bad user experience too, trying to navigate through that many rows. Look at paging them instead.


As suggested already, paging is the common way to handle large amounts of records in web apps.

But if it is the requirement to show all records in the output (e.g. for reporting purposes) then there is a way to do it.

Instead of keeping the resultset in memory on the server, you can also stream the data to the client. This can be achieved by using a DataReader instead of a DataSet and disabling all caching mechanisms.

A simple way to do this is using a SqlDataSource control for instance. Hook up the data source to a grid view as usual. Then change the DataSourceMode on the data source from DataSet to DataReader. Disable the page output cache, and you are ready to go.


That's a lot of rows! You need to apply paging for that amount of rows to be manageable. If the reason you are displaying that many rows is that you need to find certain data you should implement a search function as well.

Here's a guide from MS on how to enable paging: http://msdn.microsoft.com/en-us/library/aa479347.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜