ASP.net: GridView columns repeating after paging
<p>I have a GridView that has it's columns added dynamically in codebehind.  I've added paging to the GridView, and it works, but when it goes to the next page, it adds the columns again.</p>

<p>So the GridView starts out with 2 columns (Last Name and First Name) added from codebehind.  Then I go to it's next page, and it properly loads the next page of results, but now with 4 columns (Last Name, First Name, Last Name, First Name).  </p>

<p>What am I doing wrong here?</p>

<p>Here's the code for the GridView:</p>

<pre><code>&lt;asp:GridView id="GridView3" runat="server" AutoGenerateColumns="False"
EmptyDataText="There are no data records to display."
     AllowPaging="True" 
     OnPageIndexChanging="GridView3_PageIndexChanging"

    CssClass="GridViewStyle" GridLines="None" Width="100%"&gt;
&lt;Columns&gt;

&lt;asp:HyperLinkField DataNavigateUrlFields="EmplID"
    DataNavigateUrlFormatString="EmployeeProfile.aspx?EmplID={0}"
    DataTextField="EmplID"
    DataTextFormatString= "&lt;img src='Images/icons/document-search-result.png' alt='View'/&gt; &lt;u&gt;View&lt;/u&gt;" &gt;
    &lt;ControlStyle CssClass="titleLinksB" /&gt;
&lt;ItemStyle Wrap="False" /&gt;
&lt;/asp:HyperLinkField&gt;                   

&lt;/Columns&gt;

&lt;/asp:GridView&gt;
</code></pre>

<p>Here's the code for the codebehind:</p>

<pre><code>Private Sub loadDynamicGrid()

    Dim connetionString As String
    Dim connection As SqlConnection
    Dim command As SqlCommand
    Dim adapter As New SqlDataAdapter
    Dim ds As New DataSet
    Dim sql As String

    Dim lastName As String
    Dim linkText As String

    lastName = Request.QueryString("lastName")

    connetionString = ConfigurationManager.ConnectionStrings("dbConnectionString").ConnectionString.ToString()
    sql = "SELECT * FROM [EmployeeList] Where [lastname] like '" &amp; lastName &amp; "%' order by lastname"

    connection = New SqlConnection(connetionString)

    Try
        connection.Open()
        command = New SqlCommand(sql, connection)
        adapter.SelectCommand = command
        adapter.Fill(ds)


        Dim curLastName As New BoundField
        curLastName.HeaderText = "Last Name"
        curLastName.DataField = "LastName"
        GridView3.Columns.Insert(0, curLastName)

        Dim curFirstName As New BoundField
        curFirstName.HeaderText = "First Name"
        curFirstName.DataField = "FirstName"
        GridView3.Columns.Insert(1, curFirstName)

        GridView3.Visible = True
        GridView3.DataSource = ds
        GridView3.DataBind()

        adapter.Dispose()
        command.Dispose()
        connection.Close()

    Catch ex As Exception
        MsgBox("Can not open connection ! ")
    End Try

End Sub
</code></pre>

<p>And finally the Paging code:</p>

<pre><code>Protected Sub GridView3_PageIndexChanging(ByVal sender As [Object], ByVal e As GridViewPageEventArgs)
    GridView3.PageIndex = e.NewPageIndex
    GridView3.DataBind()
End Sub
</code></pre>

<p>Any help is greatly appreciated!</p>
