简体   繁体   中英

GridView issue with more than 1000 records

I'm having some performance issues when I return a record set with more than 1,000 records.

Sometimes the records are in upwards of 2,100 but can be as low as 10.

I have some bulk actions that I take on all the records by selecting them.

However, when the number is low, the Gridview is fine. When the record count is greater than 500 I see performance issues on the page.

What I want to happen is: if there are more than 500 records, DO NOT DISPLAY THE GRID , instead show a download button that exports to CSV or do other control things on the page.

My issue: Even if i tell it not to display the grid and instead display a message and a button, the performance is still slow.

Below is my C# code for populating the GridView. Some stuff has been removed that are unimportant and to help with readability.

How can I adjust my C# code for better performance?

SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectString"].ToString());
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SomeProcedure";
cmd.Parameters.Add(SearchParam);

try {
    DataTable GridData = new DataTable();

    conn.Open();
    using(SqlDataAdapter Sqlda = new SqlDataAdapter(cmd)) {
        Sqlda.Fill(GridData);
    }

    if (GridData.Rows.Count == 0) {
        lblSearchMsg.Text = "No Fee Records are in the Queue at this time.";
    } else {


        if (GridData.Rows.Count > 500) {
            lblSearchMsg.Text = "More than " + gridLimit.ToString() + " records returned.";
            //Show the download button

        } else {
            //Persist the table in the Session object. (for sorting)
            Session["GridData"] = GridData;

            lblRowCount.Text = "Count: " + GridData.Rows.Count.ToString();

            myGridView.DataSource = GridData;
            myGridView.DataBind();
            myGridView.Visible = true;
        }
    }

} catch (Exception ex) {
    //Do the error stuff
} finally {
    if (conn != null) {
        conn.Close();
    }
}
  1. Create a separate procedure that returns only the row count.
    Check that value not the row count of a fully retrieved data set then retrieve the full data set as needed.

  2. Keep in mind you can use the same connection to do both retrievals, no need to close the connection between calls.

  3. if you determine you need to fill a gridview and there is no need to edit the data you can read into the DataTable without the use of an adapter. Here is the basic idea modify with using statements or try/catch as you prefer:


    conn = new SqlConnection(connString);
    string query = "SELECT * FROM ....";
    SqlCommand cmd = new SqlCommand(query, conn);
    conn.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Load(dr);
    GridView1.DataSource = dt;
    GridView1.DataBind();

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