title: DataBind() on Gridview slow
body: <p>I have an application using a gridview that is really slow.</p>
  
  <p>After adding the <code>Trace=true</code> for the page, I tracked down where time is spent: when calling BindData() on a GridView. The <code>GridView</code> is connected to a <code>LinqDataSource</code>.</p>
  
  <p>Here is the trace output:</p>
  
  <pre><code>GetContact        0.955485090710761        0.000339
    DataBind        0.97438854173692         0.018903
    PopulateStates  6.58986882347249         5.615480
  </code></pre>
  
  <p>Here is the sample asp.net page</p>
  
  <pre><code>&lt;asp:LinqDataSource ContextTypeName="DAL.BALDataContext" TableName="loc_access_contacts"
      ID="_ldsContact" runat="server" OrderBy="organisation, last_name, first_name"&gt;
  &lt;/asp:LinqDataSource&gt;
  &lt;cc1:CustomGridView ID="gvContact" runat="server" DataSourceID="_ldsContact" AutoGenerateColumns="False" Width="100%"
      DataKeyNames="person_id" ForeColor="#333333" GridLines="None" AllowPaging="False"
      AllowSorting="True" BorderStyle="None" ShowFooter="false" CaptionAlign="Top"
      PagerStyle-HorizontalAlign="Left" HeaderStayPolicy="NotStay" SessionKey="Contact.GridView.Columns.SortSettings"
      SaveKey="ContactManagementSortSettings" OnRowCommand="gvContact_RowCommand" OnSorting="gvContact_Sorting"&gt;
  </code></pre>
  
  <p>In Code Behind:</p>
  
  <pre><code>Trace.Write("  DataBind");
  gvContact.DataBind();
  
  Trace.Write("  PopulateStates");
  populateStates(contact);
  </code></pre>
  
  <p>The LINQ Data Source code looks like:</p>
  
  <pre><code>public System.Data.Linq.Table&lt;loc_access_contact&gt; loc_access_contacts
  {
    get
    {
      return this.GetTable&lt;loc_access_contact&gt;();
    }
  }
  </code></pre>
  
  <p>The SQL generated from this property is a canonical <code>SELECT &lt;all fields&gt; FROM loc_access_contact</code>.</p>
  
  <p>And the table <code>loc_access_contact</code> looks like :</p>
  
  <pre><code>CREATE TABLE [dbo].[loc_access_contact](
  [person_id] [int] IDENTITY(1,1) NOT NULL,
  [organisation] [nvarchar](50) NULL,
  [last_name] [nvarchar](50) NULL,
  [first_name] [nvarchar](50) NULL,
  [is_active] [tinyint] NULL,
  [state_id] [char](2) NULL,
  [postal_code] [nchar](4) NULL,
  [town] [nvarchar](50) NOT NULL,
  [phone_number] [nvarchar](20) NULL,
  [mobile_number] [nvarchar](20) NULL,
  [fax_number] [nvarchar](20) NULL,
  [email_address] [nvarchar](255) NULL,
  [street_name] [nvarchar](255) NULL,
  [house_number] [nvarchar](20) NULL,
  [postal_box] [nvarchar](20) NULL,
  [language] [tinyint] NULL,
  CONSTRAINT [PK_person] PRIMARY KEY CLUSTERED 
  (
  [person_id] ASC
  )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  ) ON [PRIMARY]
  </code></pre>
  
  <p>The table contains about 287 contacts.</p>
  
  <p>How come the DataBind is so slow ? How can I track further down what is happening ?</p>