Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Paging in datagrid

Paging in datagrid

Scheduled Pinned Locked Moved ASP.NET
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nagraj Naik
    wrote on last edited by
    #1

    Hi all, I am using paging in datagrid and implemented paging successfully. In my application I have checkbox and Id field in datagrid,On button click I want to save Ids which having checkbox status checked. I implement solution thats is working correctly if datagrid having single page.But if I checked checkboxes from different pages in datagrid only last page Ids get saved. I mean In paging if i want to save entries of two pages i could not do that. For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) If chk.Checked = True Then lbl = DirectCast(gridItem.FindControl("ID"), Label) '''''Saving code here end if next

    S 1 Reply Last reply
    0
    • N Nagraj Naik

      Hi all, I am using paging in datagrid and implemented paging successfully. In my application I have checkbox and Id field in datagrid,On button click I want to save Ids which having checkbox status checked. I implement solution thats is working correctly if datagrid having single page.But if I checked checkboxes from different pages in datagrid only last page Ids get saved. I mean In paging if i want to save entries of two pages i could not do that. For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) If chk.Checked = True Then lbl = DirectCast(gridItem.FindControl("ID"), Label) '''''Saving code here end if next

      S Offline
      S Offline
      Suamal
      wrote on last edited by
      #2

      You can save the checkbox ids in a viewstate and get it later while submitting.Otherwise use datagrid without paging.

      N 1 Reply Last reply
      0
      • S Suamal

        You can save the checkbox ids in a viewstate and get it later while submitting.Otherwise use datagrid without paging.

        N Offline
        N Offline
        Nagraj Naik
        wrote on last edited by
        #3

        Hi, my checkbox is not binding to ant datafield.I am using viewstate to maintain Ids whose checkbox are checked but when loop For Each gridItem In DataGrid1.Items next executes that consider datagrid items count=Item present on current page and not total datagrid item present on all pages. I give some code for understanding Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Fillgrid() End If Maintain_View() Retrieve_Viewstate() End Sub Private Function Maintain_View() Dim recordArray(DataGrid1.Items.Count - 1) As String Dim n As Integer Dim gridItem As DataGridItem For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) Dim lbl As Label = DirectCast(gridItem.FindControl("ID"), Label) If chk.Checked Then recordArray(n) = lbl.Text n += 1 End If Next ViewState("recordArray") = recordArray End Function Private Function Retrieve_Viewstate() Dim recordArray(DataGrid1.Items.Count - 1) As String Dim n As Integer Dim gridItem As DataGridItem For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) Dim lbl As Label = DirectCast(gridItem.FindControl("ID"), Label) recordArray = ViewState("recordArray") For n = 0 To DataGrid1.Items.Count - 1 If recordArray(n) = lbl.Text Then chk.Checked = True End If Next Next End Function I hope you help me...

        S 1 Reply Last reply
        0
        • N Nagraj Naik

          Hi, my checkbox is not binding to ant datafield.I am using viewstate to maintain Ids whose checkbox are checked but when loop For Each gridItem In DataGrid1.Items next executes that consider datagrid items count=Item present on current page and not total datagrid item present on all pages. I give some code for understanding Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Fillgrid() End If Maintain_View() Retrieve_Viewstate() End Sub Private Function Maintain_View() Dim recordArray(DataGrid1.Items.Count - 1) As String Dim n As Integer Dim gridItem As DataGridItem For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) Dim lbl As Label = DirectCast(gridItem.FindControl("ID"), Label) If chk.Checked Then recordArray(n) = lbl.Text n += 1 End If Next ViewState("recordArray") = recordArray End Function Private Function Retrieve_Viewstate() Dim recordArray(DataGrid1.Items.Count - 1) As String Dim n As Integer Dim gridItem As DataGridItem For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) Dim lbl As Label = DirectCast(gridItem.FindControl("ID"), Label) recordArray = ViewState("recordArray") For n = 0 To DataGrid1.Items.Count - 1 If recordArray(n) = lbl.Text Then chk.Checked = True End If Next Next End Function I hope you help me...

          S Offline
          S Offline
          Suamal
          wrote on last edited by
          #4

          I think I am late to answer you.Have you solved this? Otherwise Don't maintain viewstate for each page. in not postback, initialize the viewstate (i.e clear it) 1.you have checked something in first page (2 check boxes) 2.Now you are moving to other, at that time save the checkbox's ids in viewstate. (viewstate has 2 values). 3.you have checked something in second page (3 check boxes) 4.You are moving to the third page, get the array from already existing view state and append second page's checkbox ids. (Now viewstate has 2+3 values) 5.while submitting , get it from viewstate and clear it. 6.Incase if you come to previous page,check with viewstate and populate the check boxed accordingly, if there is any change, change the viewstate value. Hope it will help you.

          N 1 Reply Last reply
          0
          • S Suamal

            I think I am late to answer you.Have you solved this? Otherwise Don't maintain viewstate for each page. in not postback, initialize the viewstate (i.e clear it) 1.you have checked something in first page (2 check boxes) 2.Now you are moving to other, at that time save the checkbox's ids in viewstate. (viewstate has 2 values). 3.you have checked something in second page (3 check boxes) 4.You are moving to the third page, get the array from already existing view state and append second page's checkbox ids. (Now viewstate has 2+3 values) 5.while submitting , get it from viewstate and clear it. 6.Incase if you come to previous page,check with viewstate and populate the check boxed accordingly, if there is any change, change the viewstate value. Hope it will help you.

            N Offline
            N Offline
            Nagraj Naik
            wrote on last edited by
            #5

            Hi, As you suggest I tried to use viewstate.But broblem is that for...loop ( For Each gridItem In DataGrid1.Items) is taking item count equals to items present in current page and not total item s in datagrid.So I can not retrive my first page values which are stored in viewstate. I am sending changed code.Please have look at it. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then fillgrid() End If Maintain_View() Retrieve_Viewstate() end sub Private Function Maintain_View() Dim recordArray(DataGrid1.Items.Count - 1) As String Dim n As Integer Dim gridItem As DataGridItem For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) Dim lbl As Label = DirectCast(gridItem.FindControl("ID"), Label) If chk.Checked Then recordArray(n) = lbl.Text ViewState.Add(lbl.Text, lbl.Text) n += 1 End If Next End Function Private Function Retrieve_Viewstate() Dim recordArray(DataGrid1.Items.Count - 1) As String Dim n As Integer Dim gridItem As DataGridItem For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) Dim lbl As Label = DirectCast(gridItem.FindControl("ID"), Label) recordArray = ViewState("recordArray") For n = 0 To DataGrid1.Items.Count - 1 If ViewState.Item(n) = lbl.Text Then chk.Checked = True End If Next Next End Function

            S 1 Reply Last reply
            0
            • N Nagraj Naik

              Hi, As you suggest I tried to use viewstate.But broblem is that for...loop ( For Each gridItem In DataGrid1.Items) is taking item count equals to items present in current page and not total item s in datagrid.So I can not retrive my first page values which are stored in viewstate. I am sending changed code.Please have look at it. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then fillgrid() End If Maintain_View() Retrieve_Viewstate() end sub Private Function Maintain_View() Dim recordArray(DataGrid1.Items.Count - 1) As String Dim n As Integer Dim gridItem As DataGridItem For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) Dim lbl As Label = DirectCast(gridItem.FindControl("ID"), Label) If chk.Checked Then recordArray(n) = lbl.Text ViewState.Add(lbl.Text, lbl.Text) n += 1 End If Next End Function Private Function Retrieve_Viewstate() Dim recordArray(DataGrid1.Items.Count - 1) As String Dim n As Integer Dim gridItem As DataGridItem For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) Dim lbl As Label = DirectCast(gridItem.FindControl("ID"), Label) recordArray = ViewState("recordArray") For n = 0 To DataGrid1.Items.Count - 1 If ViewState.Item(n) = lbl.Text Then chk.Checked = True End If Next Next End Function

              S Offline
              S Offline
              Suamal
              wrote on last edited by
              #6

              Like this you can implement it on vb.net . I jave given a rough code to do private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if(!IsPostBack) { Fillgrid(); //Initialize view state ViewState["ID"]=null; } } private void Fillgrid() { //To fill the grid } private void Maintain_View() { ArrayList ar=new ArrayList(); //Get Array List from Viewstate if(ViewState["ID"]!=null) ar=(ArrayList)ViewState["ID"]; //Check for items in the datagrid and accoring to the checkbox value //add it to arraylist foreach (DataGridItem dt in dgItems.Items) { //If check box valuw is checked { ar.Add(checkId); //Add to arrayList } } //Set it to viewstate ViewState["ID"]=ar; } private void Retrieve_View() { ArrayList ar=new ArrayList(); //Get Array List from Viewstate if(ViewState["ID"]!=null) ar=(ArrayList)ViewState["ID"]; //Compare this with datagrid items and populate the checkboxes } private void dgItems_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { //while in pagination dgItems.CurrentPageIndex =e.NewPageIndex(); Maintain_View(); Retrieve_View(); Fillgrid(); }

              N 1 Reply Last reply
              0
              • S Suamal

                Like this you can implement it on vb.net . I jave given a rough code to do private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if(!IsPostBack) { Fillgrid(); //Initialize view state ViewState["ID"]=null; } } private void Fillgrid() { //To fill the grid } private void Maintain_View() { ArrayList ar=new ArrayList(); //Get Array List from Viewstate if(ViewState["ID"]!=null) ar=(ArrayList)ViewState["ID"]; //Check for items in the datagrid and accoring to the checkbox value //add it to arraylist foreach (DataGridItem dt in dgItems.Items) { //If check box valuw is checked { ar.Add(checkId); //Add to arrayList } } //Set it to viewstate ViewState["ID"]=ar; } private void Retrieve_View() { ArrayList ar=new ArrayList(); //Get Array List from Viewstate if(ViewState["ID"]!=null) ar=(ArrayList)ViewState["ID"]; //Compare this with datagrid items and populate the checkboxes } private void dgItems_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { //while in pagination dgItems.CurrentPageIndex =e.NewPageIndex(); Maintain_View(); Retrieve_View(); Fillgrid(); }

                N Offline
                N Offline
                Nagraj Naik
                wrote on last edited by
                #7

                Hi Suamal, Sorry For trouble. As you suggest I make changes in my code. Viewstate maintain Selected id list,But Loop count(For Each gridItem In DataGrid1.Items) is for current page only that means if i select 2 entries out of 5 from first page then 2 entries saved in viewstate,next i move to next page and select 3 items from second page Retrieve_Viewstate1's code For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) Dim lbl As Label = DirectCast(gridItem.FindControl("ID"), Label) If Arr.Contains(lbl.Text) Then chk.Checked = True End If Next execute for selected 3 items from current page only. I mean For Each gridItem In DataGrid1.Items...next loop is consider for current page item count instead of total number of items in datagrid Is it correct loop or you are saying to use datagridview. I hope you keep continue to answer. I am sending latest code as below. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then fillgrid() ViewState("ID") = System.DBNull.Value End If End Sub Private Function Retrieve_Viewstate1() Dim Arr As New ArrayList() Dim n As Integer Dim gridItem As DataGridItem If Not (ViewState("ID") Is System.DBNull.Value) Then Arr = CType(ViewState("ID"), ArrayList) End If For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) Dim lbl As Label = DirectCast(gridItem.FindControl("ID"), Label) If Arr.Contains(lbl.Text) Then chk.Checked = True End If Next End Function Private Function Maintain_View1() Dim Arr As New ArrayList() Dim gridItem As DataGridItem If Not (ViewState("ID") Is System.DBNull.Value) Then Arr = CType(ViewState("ID"), ArrayList) End If For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) Dim lbl As Label = DirectCast(gridItem.FindControl("ID"), Label) If chk.Checked Then Arr.Add(lbl.Text) End If Next ViewState("ID") = Arr End Function Sub Pageing_Routine(ByVal s As Object,

                S 1 Reply Last reply
                0
                • N Nagraj Naik

                  Hi Suamal, Sorry For trouble. As you suggest I make changes in my code. Viewstate maintain Selected id list,But Loop count(For Each gridItem In DataGrid1.Items) is for current page only that means if i select 2 entries out of 5 from first page then 2 entries saved in viewstate,next i move to next page and select 3 items from second page Retrieve_Viewstate1's code For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) Dim lbl As Label = DirectCast(gridItem.FindControl("ID"), Label) If Arr.Contains(lbl.Text) Then chk.Checked = True End If Next execute for selected 3 items from current page only. I mean For Each gridItem In DataGrid1.Items...next loop is consider for current page item count instead of total number of items in datagrid Is it correct loop or you are saying to use datagridview. I hope you keep continue to answer. I am sending latest code as below. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then fillgrid() ViewState("ID") = System.DBNull.Value End If End Sub Private Function Retrieve_Viewstate1() Dim Arr As New ArrayList() Dim n As Integer Dim gridItem As DataGridItem If Not (ViewState("ID") Is System.DBNull.Value) Then Arr = CType(ViewState("ID"), ArrayList) End If For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) Dim lbl As Label = DirectCast(gridItem.FindControl("ID"), Label) If Arr.Contains(lbl.Text) Then chk.Checked = True End If Next End Function Private Function Maintain_View1() Dim Arr As New ArrayList() Dim gridItem As DataGridItem If Not (ViewState("ID") Is System.DBNull.Value) Then Arr = CType(ViewState("ID"), ArrayList) End If For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) Dim lbl As Label = DirectCast(gridItem.FindControl("ID"), Label) If chk.Checked Then Arr.Add(lbl.Text) End If Next ViewState("ID") = Arr End Function Sub Pageing_Routine(ByVal s As Object,

                  S Offline
                  S Offline
                  Suamal
                  wrote on last edited by
                  #8

                  Loop count of datagrid displays only the current page item count. But in view state we can store all. I will explain you how? #Fill grid method() 1.Populate the grid (say 100 rows with 1 to 100 as ids) clear viewstate 2.in first page select 2 checkboxes 3.In paging (to 2nd page) add 2 ids to viewstate. now it has 1,2. while retriving check for 1,2 ids in grid it will not be there. 4.In second page u select 3 add 3 ids with existing viewstate now it has 1,2,11,12,13 while retriving check for 1,2,11,12,13 ids in grid it will not be there. 5.goto 3rd page ,without selecting any ids. now viewstate has 1,2,11,12,13 6.Return to 2nd page now while retriving check for 1,2,11,12,13 ids in grid 11,12,13 ids will be there. If you change anything, you have to alter the viewstate. # in maintain view Private Function Maintain_View1() Dim Arr As New ArrayList() Dim gridItem As DataGridItem If Not (ViewState("ID") Is System.DBNull.Value) Then Arr = CType(ViewState("ID"), ArrayList) End If For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) Dim lbl As Label = DirectCast(gridItem.FindControl("ID"), Label) //If already exists then, remove it from the list If Arr.Contains(lbl.Text) Then Arr.Remove(); End If If chk.Checked Then Arr.Add(lbl.Text) End If Next ViewState("ID") = Arr End Function #call retrive view after fill GRid method Sub Pageing_Routine(ByVal s As Object, ByVal e As DataGridPageChangedEventArgs) DataGrid1.CurrentPageIndex = e.NewPageIndex Maintain_View1() Fillgrid() Retrieve_Viewstate1() End Sub

                  N 1 Reply Last reply
                  0
                  • S Suamal

                    Loop count of datagrid displays only the current page item count. But in view state we can store all. I will explain you how? #Fill grid method() 1.Populate the grid (say 100 rows with 1 to 100 as ids) clear viewstate 2.in first page select 2 checkboxes 3.In paging (to 2nd page) add 2 ids to viewstate. now it has 1,2. while retriving check for 1,2 ids in grid it will not be there. 4.In second page u select 3 add 3 ids with existing viewstate now it has 1,2,11,12,13 while retriving check for 1,2,11,12,13 ids in grid it will not be there. 5.goto 3rd page ,without selecting any ids. now viewstate has 1,2,11,12,13 6.Return to 2nd page now while retriving check for 1,2,11,12,13 ids in grid 11,12,13 ids will be there. If you change anything, you have to alter the viewstate. # in maintain view Private Function Maintain_View1() Dim Arr As New ArrayList() Dim gridItem As DataGridItem If Not (ViewState("ID") Is System.DBNull.Value) Then Arr = CType(ViewState("ID"), ArrayList) End If For Each gridItem In DataGrid1.Items Dim chk As CheckBox = DirectCast(gridItem.FindControl("Status"), CheckBox) Dim lbl As Label = DirectCast(gridItem.FindControl("ID"), Label) //If already exists then, remove it from the list If Arr.Contains(lbl.Text) Then Arr.Remove(); End If If chk.Checked Then Arr.Add(lbl.Text) End If Next ViewState("ID") = Arr End Function #call retrive view after fill GRid method Sub Pageing_Routine(ByVal s As Object, ByVal e As DataGridPageChangedEventArgs) DataGrid1.CurrentPageIndex = e.NewPageIndex Maintain_View1() Fillgrid() Retrieve_Viewstate1() End Sub

                    N Offline
                    N Offline
                    Nagraj Naik
                    wrote on last edited by
                    #9

                    Great!!!!!!! You are Genius!!!!!!! I don't have words to say thanks to you.I really Grateful to you. I hope we will meet soon with some more.......... Best Regards, Nagraj Teach Life To Laugh........:laugh:

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups