GridView display more then 1000 rows - c#

I use GridView to display data from DB. First I query data from DB to list<list<string>>, sort use linq, copy this array to DataTable and bind DataTable to GridView. But if in DataTable more then 1000 rows - it works very long or not works (error in browser).
How to fix this?
UPDATE i use rowspan in columns and create a delete buttons to all rows, and if i use paging wiil it works?
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnDataBinding="GridView1_DataBinding" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting" >
<Columns>
<asp:BoundField DataField="Number" ItemStyle-Width="200px" > <ItemStyle Width="200px" > </ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Name" ItemStyle-Width="200px" > <ItemStyle Width="200px" > </ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Nameoid" ItemStyle-Width="200px" > <ItemStyle Width="200px" > </ItemStyle>
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="return DeleteConfirm();" OnClick="Button2_Click" />
<asp:HiddenField ID="HiddenField2" runat="server" Value='<%#Bind("Number") %>' />
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

It seems like you are doing lots of server side code in one round trip time. You should make it lesser so that page could show the result in grid view.
Here are few things on which you should work:
There is no need to store first in list then data table and then sorting. You can directly use the DATAVIEW class to sort the result from DB.
I think you should not display all 1000 record in grid view in a single page. You should use PAGING in grid view. This is also in built functionality of grid view by this you can show number of record in a one grid view page as you want..
For reference how to do this there is complete example. Follow the below link
http://www.dotnetgallery.com/kb/resource12-How-to-implement-paging-and-sorting-in-aspnet-Gridview-control.aspx
UPDATE:
If you want to delete a row in gridview with a link button on each row. Then follow the below article
enter link description here
You can put the UpdatePanel outside of gridview. First try to use the above code (from link), then try to add update panel. If you feel any difficulties ask here.

My suggestion would be to use Lazy loading pattern with your GridView .
You can refer to :
LazyLoadUpdatePanelusingTimerControlAJAX
or you can implement custom paging
Custompaging

You can use paging in your grid view having grid size of say 10,20,30 to accommodate large amount of data so that the page doesn't crash.
Try referring to this example
http://www.codeproject.com/Articles/106678/Display-Large-Amount-of-Data-in-GridView-with-Sear

Related

reverse asp.net grid view columns header into rows header

I'm still learning asp.net and I want to ask you about how to reverse the grid view, I mean the default grid view would be like this
and this is the code for it:
<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True"
AutoGenerateColumns="false" Width="256px">
<Columns>
<asp:TemplateField HeaderText="1"> <ItemTemplate>
<asp:Label ID="test1" runat="server" Text="test1"></asp:Label>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="2"> <ItemTemplate>
<asp:Label ID="test2" runat="server" Text="test2"></asp:Label>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="3"> <ItemTemplate>
<asp:Label ID="test3" runat="server" Text="test3"></asp:Label>
</ItemTemplate></asp:TemplateField> </Columns>
<RowStyle HorizontalAlign="Left" VerticalAlign="Bottom" />
</asp:GridView>
and i want it to be like this picture :
the test here will be data bound and the numbers should be header text, this is only for test, is there a way to reverse it?
thank you all for your valuable advises and efforts, I really appreciate it.
I was able to achieve what I want by pivoting the data table and the columns became rows and the opposite.
this link can explain it.
thx

How to encode html data in asp / vb.net boundcolumn datagrid?

I have some data in database which I am binding it to a bound column.
Data contains many html tags.
But I want to display it as TEXT only "NOT TO RENDER IT"
I am not sure because HTMLENCODE property doesn't work with boundcolumn.
Please help.
Also just to add I have multiple datagrids binding with bind() function and are using one event handler Itemdatabound.
<asp:boundcolumn datafield="content" readonly="True" headertext="Product ID ">
<headerstyle horizontalalign="Center" width="100px"></headerstyle>
<itemstyle horizontalalign="Left" width="100px"></itemstyle>
</asp:boundcolumn>
Prevent HTML encoding in auto-generated GridView columns
extract from possible solution from above link:
<asp:TemplateField HeaderText="myLink" SortExpression="myLink">
<ItemTemplate>
<asp:Literal ID="litHyperLink" runat="server" Text='<%# Bind("myLink", "{0}") %>' />
</ItemTemplate>
</asp:TemplateField>
Or bind your own columns, also shown in above link
Server.HtmlDecode()
This will allow you to print the tags without rendering them to html.
http://msdn.microsoft.com/en-us/library/hwzhtkke(v=vs.110).aspx

Gridview disable edit on 1 column asp.net

I am using a gridview Edit to edit the values i have in my gridview, when i press edit, all columns can be edited, i would like that one of the columns is not allowed to be edited.
Is there any way i can do this?
Thiss is my aspx code:
<asp:GridView
ID="GridView1"
runat="server"
AllowSorting="True"
OnRowCommand="GridView1_SelectedIndexChanged1"
AutoGenerateEditButton="True"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
CellSpacing="10"
OnRowUpdating="GridView1_RowUpdating" ShowFooter="True"
onselectedindexchanged="GridView1_SelectedIndexChanged"
OnRowEditing="GridView1_RowEditing">
</asp:GridView>
Thanks
If you are using asp:BoundField, try
<asp:BoundField DataField="CustomerID"
ReadOnly="true"
HeaderText="Customer ID"/>
Or else if you are using asp:TemplateField, you can either
Render it in either an asp:Label inside EditItemTemplate
Omit the EditItemTemplate for that column altogether
Sure, make use of the EditItemTemplate. In the following example field ID will not be edited in the Edit mode:
<asp:GridView runat="server">
<Columns>
...
<asp:TemplateField HeaderText="ID">
<EditItemTemplate>
<%# Eval("ID") %>
</EditItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
please show some markup. Quick and dirty I think depending on your markup aspx you can remove the textbox from the EditItem template for the column you want to prevent editing... there are also other solutions of course :)
If you are using template field
((TemplateField)gvGridView.Columns[index]).EditItemTemplate = null;
if boundfield
((BoundField)gvGridView.Columns[index]).ReadOnly = true;

How to programmatically iterate through pages of a GridView

I've created a wallboard application to display outstanding support calls for my ICT department. I've bound a number of gridviews to sqldatasources which execute a stored procedure. This is automated via asp.net ajax controls and partially refreshes the page/data every 30 seconds.
At the moment, when the number of records in the gridview goes over 9, the gridview automatically pages and shows the number of pages in the bottom right hand corner. The helpdesk can then VNC to the box which controls the screen and manually click to see what's on the next page.
What I am after is a way to programmatically (using the c# code-behind file) changing the current displayed page after 10/15 seconds or so, obviously if this is possible in the scope of the gridview. I trailed using javascript (and failed at jquery) of scrolling the gridview within a div, however this didn't work as expected.
Can anyone point me in the right example? I can't find anyone else querying this functionality via a quick Google. Any help/advice of how to fix this issue would be greatly appreciated!!
Gridview Code:
<asp:GridView ID="GridView1" ShowHeader="False" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
GridLines="None" CellPadding="2" Font-Size="35pt" AllowPaging="True" PageSize="9">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID">
<ItemStyle Width="15%" />
</asp:BoundField>
<asp:BoundField DataField="ASSIGNEES" HeaderText="ASSIGNEES" SortExpression="ASSIGNEES">
<ItemStyle Width="32%" Wrap="false"/>
</asp:BoundField>
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title">
<ItemStyle Width="53%" Wrap="false"/>
</asp:BoundField>
</Columns>
</asp:GridView>
SqlDataSource Code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:FPConnectionString %>" SelectCommand="HDMonitoringOutstandingToday" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
Printscreen of wallboard:
You can try something like this in a timer.
if(GridView1.PageIndex == GridView1.PageCount)
{
GridView1.PageIndex = 0;
}
else
{
GridView.PageIndex = GridView.PageIndex + 1;
}
I cant remember if you would need to add one to the PageIndex or not.
But anyway, the properties you need to work with are PageIndex and PageCount.
GridView.PageIndex
You can change pages by setting the PageIndex, how you do it is up to you see here for some examples: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.pageindex.aspx

Troubles retrieving data from within OnRowCommand from a GridViews

If I have this <asp:ButtonField runat="server" DataTextField="Name" CommandName="GetName"></asp:ButonField> Within a GridView. Is there any way to retrieve the DataTextField (Name) from the OnRowCommand method?
<asp:GridView ID="GridView1" runat="server"
AllowPaging="True" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource_Names"
DataKeyNames="ID,ModuleId" OnRowCommand="ChangeName">
Or alternatively, is there any way to make CommandName into an attribute where the command is dynamically entered based on given data, similar to the difference between DataTextField vs TextField.
I added <asp:BoundField DataField="Name" /> Then you can retrieve it using the same method here: GridView.onRowCommand Method
All I need to do now is find out a way to hide the field. If anyone knows how to, please let me know. Visible="false" gets rid of the field all together...
Not really sure if this is the best way, but it works:
Make your column a templatefield and bind the name value to a asp:hiddenfield control:
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hfName" runat="server" Value='<%# Eval("Name") %>'>
</asp:HiddenField>
</ItemTemplate>
</asp:TemplateField>

Categories

Resources