title: Best way to load data from DB and show in GridView
body: <p>I am newbie to the ASP.Net world and have a confusion on how to approach the below scenario.</p>
  
  <p>In my application I have to fetch the data from the database when page is loaded and show this in a GridView. The table currently has around 1000 records with about 7 columns. But the data will keep growing. </p>
  
  <p>Here is the code of how I am binding the data to the grid.</p>
  
  <pre><code> protected void Page_Load(object sender, EventArgs e)
   {
       var data = new AppsAuthData().GetAllUsers();
       gridUsersInfo.DataSource = data;
       gridUsersInfo.DataBind();
   }
  </code></pre>
  
  <p>I came to know that on every post back above code is getting executed (which obviously is not good). So I added the following to that start of the function</p>
  
  <pre><code>if (IsPostBack)
       return;
  var data = new AppsAuthData().GetAllUsers();
  gridUsersInfo.DataSource = data;
  gridUsersInfo.DataBind();
  </code></pre>
  
  <p>Page Markup</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.Master" CodeBehind="RemoveUsers.aspx.cs" Inherits="AppsAuth.Authencations.RemoveUsers" %&gt;
  </code></pre>
  
  <p>This again has an issue that after Postbacks, GridView has nothing to show. So next I saved my results to the ViewState and on each post back i was retrieving/updating the ViewState.</p>
  
  <p>But since data can be huge in some scenarios, so what are best options available to deal with such issues?</p>
  
  <p>GridView snippet</p>
  
  <pre><code> &lt;asp:GridView ID="gridUsersInfo" runat="server" Width="100%" ForeColor="#333333" AllowPaging="True"
          AllowSorting="True" AutoGenerateColumns="false" OnSorting="UserInfo_Sorting" OnRowEditing="gridUsersInfo_RowEditing"
          OnPageIndexChanging="gridUsersInfo_PageIndexChanging" AutoGenerateEditButton="True"&gt;
   &gt;    &lt;Columns&gt;
              &lt;asp:BoundField DataField="USER_ID" ReadOnly="True" HeaderText="ID" SortExpression="USER_ID" /&gt;
              &lt;asp:BoundField DataField="USER_NAME" ReadOnly="False" HeaderText="User Name" SortExpression="USER_NAME" /&gt;
          &lt;/Columns&gt;
      &lt;/asp:GridView&gt;
  
  protected void gridUsersInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
      {
          gridUsersInfo.PageIndex = e.NewPageIndex;
          gridUsersInfo.DataBind();
      }
  </code></pre>