Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7511887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:41:55+00:00 2026-05-29T23:41:55+00:00

I have a GridView that has it’s columns added dynamically in codebehind. I’ve added

  • 0

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.

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).

What am I doing wrong here?

Here’s the code for the GridView:

<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%">
<Columns>

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

</Columns>

</asp:GridView>

Here’s the code for the codebehind:

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 '" & lastName & "%' 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

And finally the Paging code:

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

Any help is greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:41:58+00:00Added an answer on May 29, 2026 at 11:41 pm

    I assume that you’re calling loadDynamicGrid on every postback and not only If Not Page.IsPostBack.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            loadDynamicGrid()
        End If
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a gridview that has an asp:ButtonField as one of the columns.
I have an ASP.NET GridView which has columns that look like this: | Foo
I have an ASP.NET GridView that has four columns. The first three are typical
I have a aspx page that has a GridView with a datasource of an
I have a GridView that has multiple columns. Let's say I have a business
I have a page, lets call it SourceTypes.aspx, that has a a GridView that
I have a gridview table that has three columns..fileID, uploadedBy and delete. Only the
I have a GridView on an ASP.NET page with a TemplateField column that has
I have an ASP.NET page that has dynamically created LinkButton elements. Please note that
I have a gridview that has some 20 columns and 1000 rows. The grid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.