Slow datagrid on postback

Discussion in 'ASP .Net Datagrid Control' started by OT Solutions, sales, Apr 21, 2005.

  1. Hello there,

    I have a datagrid that has a select button (so the user can choose a
    row) and also uses the datagrid sort.

    If I have a smaller grid (say 150 rows) the response time of the sort
    or select button (that just opens a popup with some information) is a
    couple of seconds.

    If I load in say 500 records into the datagrid (the load speed is
    fine). As soon as I try and sort on a column, or select from the
    grid, it takes about 20 seconds.

    Does anyone know why this would be happening.

    Any help would be appreciated,

    Tom Olthoff

    Here is a sample of the code:

    <asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px;
    POSITION: absolute; TOP: 80px" runat="server"
    CellPadding="2" CellSpacing="2" AllowPaging="True"
    AllowSorting="True" Font-Names="Arial"
    Font-Size="Smaller" PageSize="1000" AutoGenerateColumns="False"
    OnSortCommand="SortResults"
    OnPageIndexChanged="PageResults"
    OnItemDataBound="DataGrid1_ItemDataBound"
    OnSelectedIndexChanged="DataGrid1_SelectedIndexChanged">
    <SelectedItemStyle BackColor="#8080FF"></SelectedItemStyle>
    <HeaderStyle BackColor="Silver"></HeaderStyle>
    <Columns>
    <asp:ButtonColumn Visible="False" Text="Select"
    CommandName="Select"></asp:ButtonColumn>
    <asp:BoundColumn DataField="PositionCode"
    SortExpression="PositionCode" HeaderText="Position
    Code"></asp:BoundColumn>
    <asp:BoundColumn DataField="UserName" SortExpression="UserName"
    HeaderText="User Name"></asp:BoundColumn>
    <asp:BoundColumn DataField="Dept" SortExpression="Dept"
    HeaderText="Department"></asp:BoundColumn>
    <asp:BoundColumn DataField="Div" SortExpression="Div"
    HeaderText="Division"></asp:BoundColumn>
    <asp:BoundColumn DataField="ReplCycle" SortExpression="ReplCycle"
    HeaderText="Replacement Cycle"></asp:BoundColumn>
    <asp:BoundColumn DataField="ReplPolicy"
    SortExpression="ReplPolicy" HeaderText="Replacement
    Policy"></asp:BoundColumn>
    </Columns>
    <PagerStyle Visible="False" NextPageText="Next 5"
    PrevPageText="Prev 5"></PagerStyle>
    </asp:datagrid>


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here

    If Not Page.IsPostBack Then
    BindData()
    End If

    End Sub

    Sub BindData()
    '1. Create a connection
    Dim ConnectionString As String
    Dim CommandText As String
    Dim NewDV As DataView
    Dim strSort As String
    Dim strFilter As String

    CommandText = "usp_GetPCs"

    ' Grab the connection from the global.asax file.
    ' (This is where the application variables are set)
    ConnectionString = Application("sqlConnectionString")

    Dim ds As New DataSet

    ds = ViewState("DataSet")

    If ds Is Nothing Then
    Dim myConnection As New SqlConnection(ConnectionString)
    Dim myCommand As New SqlDataAdapter(CommandText,
    myConnection)
    Dim ds2 As New DataSet
    myCommand.Fill(ds2)
    NewDV = ds2.Tables(0).DefaultView()
    'Before DataBind - Add New Rows Here for Each Sub Heading
    'We'll come back to this shortly!
    strSort = "Dept, PositionCode"
    ViewState("DataSet") = ds2
    Else
    strSort = ViewState("DataSort")
    NewDV = ds.Tables(0).DefaultView()
    ViewState("DataSet") = ds
    End If

    NewDV.Sort = strSort

    DataGrid1.DataSource = NewDV
    DataGrid1.DataBind()


    ViewState("DataSort") = strSort

    End Sub

    Sub SortResults(ByVal sender As Object, ByVal e As
    DataGridSortCommandEventArgs)
    Dim strSort
    Dim NewDS As DataSet
    Dim NewDV As DataView

    strSort = e.SortExpression

    NewDS = ViewState("DataSet")
    NewDV = NewDS.Tables(0).DefaultView
    NewDV.Sort = strSort
    ViewState("DataSort") = strSort
    BindData()

    End Sub


    Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
    DataGridItemEventArgs)
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
    ListItemType.AlternatingItem Then
    e.Item.Attributes.Add("onmouseover",
    "this.style.backgroundColor='Silver'")
    e.Item.Attributes.Add("onmouseout",
    "this.style.backgroundColor='White'")

    Dim button As LinkButton = _
    CType(e.Item.Cells(0).Controls(0), LinkButton)
    e.Item.Attributes("onclick") = _
    Page.GetPostBackClientHyperlink(button, "")

    End If

    End Sub



    Sub EditRecord(ByVal PositionCode As Integer)


    ' Response.Redirect("AddActionType.aspx?ActionType_ID="
    & ID)
    ' Response.Redirect("AddActionType.aspx?ActionType_ID="
    & ID)

    ' Open the positionlist.aspx screen passing in the position
    code
    Response.Write("<script language='javascript'> {
    window.open('PositionList.aspx?PositionCode=" & CStr(PositionCode) &
    "'" & ",'PosWin','toolbar=no, height=690, width=990 ,top=20,left=20')
    }</script>")
    End Sub


    Public Sub DataGrid1_SelectedIndexChanged(ByVal sender As
    System.Object, ByVal e As System.EventArgs) Handles
    DataGrid1.SelectedIndexChanged
    ' This is the event for the datagrid selection change (by
    mouse click)
    Dim ID As String
    ID = DataGrid1.SelectedItem.Cells(1).Text
    EditRecord(Val(ID))
    End Sub
     
    OT Solutions, sales, Apr 21, 2005
    #1
    1. Advertising

  2. OT Solutions, sales

    Brock Allen Guest

    The real question is what mechanism are you usiung for sorting. That's what
    you should focus on or perhaps alternate mechanisms.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > Hello there,
    >
    > I have a datagrid that has a select button (so the user can choose a
    > row) and also uses the datagrid sort.
    >
    > If I have a smaller grid (say 150 rows) the response time of the sort
    > or select button (that just opens a popup with some information) is a
    > couple of seconds.
    >
    > If I load in say 500 records into the datagrid (the load speed is
    > fine). As soon as I try and sort on a column, or select from the
    > grid, it takes about 20 seconds.
    >
    > Does anyone know why this would be happening.
    >
    > Any help would be appreciated,
    >
    > Tom Olthoff
    >
    > Here is a sample of the code:
    >
    > <asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px;
    > POSITION: absolute; TOP: 80px" runat="server"
    > CellPadding="2" CellSpacing="2" AllowPaging="True"
    > AllowSorting="True" Font-Names="Arial"
    > Font-Size="Smaller" PageSize="1000" AutoGenerateColumns="False"
    > OnSortCommand="SortResults"
    > OnPageIndexChanged="PageResults"
    > OnItemDataBound="DataGrid1_ItemDataBound"
    > OnSelectedIndexChanged="DataGrid1_SelectedIndexChanged">
    > <SelectedItemStyle BackColor="#8080FF"></SelectedItemStyle>
    > <HeaderStyle BackColor="Silver"></HeaderStyle>
    > <Columns>
    > <asp:ButtonColumn Visible="False" Text="Select"
    > CommandName="Select"></asp:ButtonColumn>
    > <asp:BoundColumn DataField="PositionCode"
    > SortExpression="PositionCode" HeaderText="Position
    > Code"></asp:BoundColumn>
    > <asp:BoundColumn DataField="UserName" SortExpression="UserName"
    > HeaderText="User Name"></asp:BoundColumn>
    > <asp:BoundColumn DataField="Dept" SortExpression="Dept"
    > HeaderText="Department"></asp:BoundColumn>
    > <asp:BoundColumn DataField="Div" SortExpression="Div"
    > HeaderText="Division"></asp:BoundColumn>
    > <asp:BoundColumn DataField="ReplCycle" SortExpression="ReplCycle"
    > HeaderText="Replacement Cycle"></asp:BoundColumn>
    > <asp:BoundColumn DataField="ReplPolicy"
    > SortExpression="ReplPolicy" HeaderText="Replacement
    > Policy"></asp:BoundColumn>
    > </Columns>
    > <PagerStyle Visible="False" NextPageText="Next 5"
    > PrevPageText="Prev 5"></PagerStyle>
    > </asp:datagrid>
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > 'Put user code to initialize the page here
    > If Not Page.IsPostBack Then
    > BindData()
    > End If
    > End Sub
    >
    > Sub BindData()
    > '1. Create a connection
    > Dim ConnectionString As String
    > Dim CommandText As String
    > Dim NewDV As DataView
    > Dim strSort As String
    > Dim strFilter As String
    > CommandText = "usp_GetPCs"
    >
    > ' Grab the connection from the global.asax file.
    > ' (This is where the application variables are set)
    > ConnectionString = Application("sqlConnectionString")
    > Dim ds As New DataSet
    >
    > ds = ViewState("DataSet")
    >
    > If ds Is Nothing Then
    > Dim myConnection As New SqlConnection(ConnectionString)
    > Dim myCommand As New SqlDataAdapter(CommandText,
    > myConnection)
    > Dim ds2 As New DataSet
    > myCommand.Fill(ds2)
    > NewDV = ds2.Tables(0).DefaultView()
    > 'Before DataBind - Add New Rows Here for Each Sub Heading
    > 'We'll come back to this shortly!
    > strSort = "Dept, PositionCode"
    > ViewState("DataSet") = ds2
    > Else
    > strSort = ViewState("DataSort")
    > NewDV = ds.Tables(0).DefaultView()
    > ViewState("DataSet") = ds
    > End If
    > NewDV.Sort = strSort
    >
    > DataGrid1.DataSource = NewDV
    > DataGrid1.DataBind()
    > ViewState("DataSort") = strSort
    >
    > End Sub
    >
    > Sub SortResults(ByVal sender As Object, ByVal e As
    > DataGridSortCommandEventArgs)
    > Dim strSort
    > Dim NewDS As DataSet
    > Dim NewDV As DataView
    > strSort = e.SortExpression
    >
    > NewDS = ViewState("DataSet")
    > NewDV = NewDS.Tables(0).DefaultView
    > NewDV.Sort = strSort
    > ViewState("DataSort") = strSort
    > BindData()
    > End Sub
    >
    > Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
    > DataGridItemEventArgs)
    > If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
    > ListItemType.AlternatingItem Then
    > e.Item.Attributes.Add("onmouseover",
    > "this.style.backgroundColor='Silver'")
    > e.Item.Attributes.Add("onmouseout",
    > "this.style.backgroundColor='White'")
    > Dim button As LinkButton = _
    > CType(e.Item.Cells(0).Controls(0), LinkButton)
    > e.Item.Attributes("onclick") = _
    > Page.GetPostBackClientHyperlink(button, "")
    > End If
    >
    > End Sub
    >
    > Sub EditRecord(ByVal PositionCode As Integer)
    >
    > ' Response.Redirect("AddActionType.aspx?ActionType_ID="
    > & ID)
    > ' Response.Redirect("AddActionType.aspx?ActionType_ID="
    > & ID)
    > ' Open the positionlist.aspx screen passing in the position
    > code
    > Response.Write("<script language='javascript'> {
    > window.open('PositionList.aspx?PositionCode=" & CStr(PositionCode) &
    > "'" & ",'PosWin','toolbar=no, height=690, width=990 ,top=20,left=20')
    > }</script>")
    > End Sub
    > Public Sub DataGrid1_SelectedIndexChanged(ByVal sender As
    > System.Object, ByVal e As System.EventArgs) Handles
    > DataGrid1.SelectedIndexChanged
    > ' This is the event for the datagrid selection change (by
    > mouse click)
    > Dim ID As String
    > ID = DataGrid1.SelectedItem.Cells(1).Text
    > EditRecord(Val(ID))
    > End Sub
     
    Brock Allen, Apr 21, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    3,047
  2. HK
    Replies:
    3
    Views:
    458
  3. mike
    Replies:
    3
    Views:
    399
    Virgil Green
    Jul 11, 2005
  4. JosephByrns

    Slow, then quick then slow

    JosephByrns, Jul 10, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    2,498
    codezilla94
    Nov 13, 2007
  5. con

    SLOW oh so slow

    con, May 26, 2008, in forum: ASP .Net
    Replies:
    4
    Views:
    431
Loading...

Share This Page