Results 1 to 16 of 16

Thread: [RESOLVED] Gridview Paging

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Resolved [RESOLVED] Gridview Paging

    I have a web application which does data filtering. The return data set will be bind to Datagridview with Paging set to True.

    vb Code:
    1. Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Using connection As New SqlConnection("Data Source=localhost\sqlexpress;Initial Catalog=26may;Integrated Security=SSPI;")
    3.             Dim criteria As String = String.Empty
    4.             Dim sql As String = "SELECT pk_ddlvryMasterid,pk_ddlvryno as [Dlvry No],convert(varchar,entereddate,106) as Dated" & _
    5.                     ", lpono as LPO_No, Supplier FROM qDDlvryMaster"
    6.  
    7.             Select Case criteriaDropDownList.Text.ToUpper
    8.                 Case "DLVRY NO" : criteria = "PK_DDLVRYNO"
    9.                 Case "LPO NO" : criteria = "LPONO"
    10.                 Case "SUPPLIER" : criteria = "SUPPLIER"
    11.             End Select
    12.            
    13.             Using command As New SqlCommand(sql & " where " & criteria & " LIKE @value + '%'", connection)
    14.                 command.Parameters.Add("@value", Data.SqlDbType.VarChar).Value = valueTextBox.Text.ToString
    15.  
    16.                 connection.Open()
    17.                 Using reader As SqlDataReader = command.ExecuteReader()
    18.                     Dim tb As New DataTable
    19.                     tb.Load(reader)
    20.                     GridView1.DataSource = tb 'set datasource
    21.                     GridView1.DataBind() 'bind to gridview to view return data set
    22.                 End Using
    23.  
    24.  
    25.             End Using
    26.         End Using
    27.  
    28.     End Sub
    29.  
    30.  
    31.     Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging
    32.  
    33.         GridView1.PageIndex = e.NewPageIndex
    34.         GridView1.DataBind()
    35.  
    36.     End Sub

    The code works perfectly but every time I move to different page the gridview clear all the rows.

    And also how can I make the first gridview column create link to its details page?
    Last edited by jlbantang; Jul 17th, 2010 at 03:43 AM.

  2. #2
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Gridview Paging

    Quote Originally Posted by jlbantang View Post
    The code works perfectly but every time I move to different page the gridview clear all the rows.
    Code:
    'do this in page load event
    if not Me.IsPostBack then
    'bind the gridview
    end if
    Quote Originally Posted by jlbantang View Post
    And also how can I make the first gridview column create link to its details page?
    withing the <itemTemplate> take a linkbutton and then you can easily access the link button

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: Gridview Paging

    hey,

    I added like this:
    vb Code:
    1. If Me.IsPostBack Then
    2.             Call Button1_Click(sender, e) 'call button to fillin datagrid
    3.         End If

    Also I added the linkbutton inside the Grid ItemTemplate but it did not do anything when the page re-run.

    I want to pass the column pk_ddlvryMasterid value to the details page. Im trying to look at ASP.NET website for some tutorial similar to this regard but I cant find one. Do you any graphical/video tutorial similar to this thread?

    BTW columns are not defined in gridview.

    Thanks.

  4. #4
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: Gridview Paging

    hay,

    have a look
    http://msdn.microsoft.com/en-us/library/ms972948.aspx
    http://www.codeproject.com/KB/webfor..._GridView.aspx

    you can use the Item templates to add any thing to your GridView.
    and also, your grid didn't show any thing after paging because your Grid must have its Datasource after the reload,

    Code:
        Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging
     
            GridView1.PageIndex = e.NewPageIndex
            GridView1.DataBind()
        End Sub
    in this code you didn't give your GridView the Datasource
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  5. #5
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: Gridview Paging

    i believe that the HOWTo will work also
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  6. #6
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Gridview Paging

    Quote Originally Posted by jlbantang View Post
    hey,

    vb Code:
    1. If Me.IsPostBack Then
    2.             Call Button1_Click(sender, e) 'call button to fillin datagrid
    3.         End If
    try to avoid this code;i want to tell that create a function and then call that function from where ever you want.........e.g;from the button click,page load,etc etc

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Gridview Paging

    Quote Originally Posted by jlbantang View Post
    And also how can I make the first gridview column create link to its details page?
    Have a look here:

    http://www.asp.net/data-access/tutor...-detailview-cs

    Gary

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: Gridview Paging

    Quote Originally Posted by HowTo View Post
    try to avoid this code;i want to tell that create a function and then call that function from where ever you want.........e.g;from the button click,page load,etc etc

    Can you please tell me why should I avoid the code when the function Im going to create will perform the same task.

  9. #9
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: Gridview Paging

    hay,
    there is no harm from doing this, i believe he means you can use these lines of codes in any place you want.

    do you still have problems ??
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Gridview Paging

    What HowTo is referring to, is that you shouldn't directly call the click event of a button.

    If you need to repeat the code that is executed in a button, move that code into it's own method. Then call this method from button the click event, and also in other places that you need it.

    Does that make sense?

    Gary

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: Gridview Paging

    hey,

    Yes indeed . If I am correct you just mention what he say and did not reason out the difference. I am thinking of someone answer the WHY SHOULD WE DO THAT?

  12. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Gridview Paging

    The reason is quite simply it is bad practice.

    When you call the click event directly, you don't actually know the value of sender and e, these are normally created for you, when the click event is invoked. However, in calling it directly, you either need to create these properties, which you can't really do, or set them equal to null.

    In Windows Form world, there is the PerformClick method on a button, which can be used to perform the action of clicking the button, but this does not exist in the world of ASP.Net.

    Gary

  13. #13
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Gridview Paging

    Quote Originally Posted by gep13 View Post
    The reason is quite simply it is bad practice.

    When you call the click event directly, you don't actually know the value of sender and e, these are normally created for you, when the click event is invoked. However, in calling it directly, you either need to create these properties, which you can't really do, or set them equal to null.

    In Windows Form world, there is the PerformClick method on a button, which can be used to perform the action of clicking the button, but this does not exist in the world of ASP.Net.

    Gary


    thats exactly what i tried to mean but i was a bit late on this thread to explain him why should not we do that

    thanks Gary

  14. #14
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Gridview Paging

    Quote Originally Posted by HowTo View Post


    thats exactly what i tried to mean but i was a bit late on this thread to explain him why should not we do that

    thanks Gary
    No probs.

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: Gridview Paging

    It mean a lotta things.

    thanks.

  16. #16
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Gridview Paging

    Hey,

    I am glad to hear that you got this sorted out.

    Out of interest, what was the solution to your problem? It will help other people in the community.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width