Sunday, August 11, 2019

How to implement Custom Paging in ASP.Net GridView using SQL Server Stored Procedure

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for fetch data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok. 
In this example, I have used one tables as below

Step-4: Create a Stored Procedure in Sql Server for efficiently retrieve only the specific rows.

Open Database > Right Click on Stored Procedure > Add New Stored Procedure > Write below Sql Code > Save. 

  1. CREATE PROCEDURE dbo.getData
  2. @PageNo int,
  3. @NoOfRecord int,
  4. @TotalRecord int output
  5. AS
  6. -- Here Get Total No of record
  7. Select @TotalRecord = Count(*) from CityData
  8.  
  9. Select * from
  10. (
  11. Select
  12. Row_number() over( Order by SLID ASC) as RowNo,
  13. SLID,
  14. Country,
  15. State,
  16. City
  17. From
  18. CityData
  19. ) as Tab
  20. Where
  21. Tab.RowNo between ((@PageNo - 1) * @NoOfRecord) + 1 and (@PageNo * @NoOfRecord)
  22. Order by SLID ASC
  23.  
  24. RETURN
  25.  

Step-5: Add a Webpage and Design for Show Data in Gridview With Custom Paging option

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

HTML Code 
  1. <h3>Custom paging in ASP.NET Gridview</h3><br />
  2. <div>
  3.  
  4. <asp:GridView ID="GridView1" runat="server" CellSpacing="5" AutoGenerateColumns="false" Width="442px">
  5. <Columns>
  6. <asp:BoundField DataField="SLID" HeaderText="SL No." />
  7. <asp:BoundField DataField="Country" HeaderText="Country Name" />
  8. <asp:BoundField DataField="State" HeaderText="State Name" />
  9. <asp:BoundField DataField="City" HeaderText="City Name" />
  10. </Columns>
  11. </asp:GridView>
  12. <%-- Here panel placed for contain Custom button for paging --%>
  13. <asp:Panel ID="Panel1" runat="server"></asp:Panel>
  14. </div>

Step-6: Write following code in Page_Load event for Show data in Gridview and Genarate button for Custom Paging.


  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. PopulateData(1, 5);
  6. }
  7.  
  8. AddpagingButton();
  9. }

Step-7: Write below function for fetch & Show data in Gridview.

  1. private void PopulateData(int pageNo, int noOfRecord)
  2. {
  3. // this method is for getting data from database based on selected page
  4. using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString))
  5. {
  6. SqlCommand cmd = new SqlCommand("getData", con);
  7. cmd.CommandType = System.Data.CommandType.StoredProcedure;
  8. cmd.Parameters.AddWithValue("@PageNo", pageNo);
  9. cmd.Parameters.AddWithValue("@NoOfRecord", noOfRecord);
  10.  
  11. SqlParameter TotalRecordSP = new SqlParameter("@TotalRecord", System.Data.SqlDbType.Int);
  12. TotalRecordSP.Direction = System.Data.ParameterDirection.Output;
  13. cmd.Parameters.Add(TotalRecordSP);
  14.  
  15. DataTable dt = new DataTable();
  16. if (con.State != ConnectionState.Open)
  17. {
  18. con.Open();
  19. }
  20. dt.Load(cmd.ExecuteReader());
  21.  
  22. int totalRecord = 0;
  23. if (TotalRecordSP.Value != null)
  24. {
  25. int.TryParse(TotalRecordSP.Value.ToString(), out totalRecord);
  26. }
  27. GridView1.DataSource = dt;
  28. GridView1.DataBind();
  29.  
  30. // Store Total Record & No of record per page into view state for use in Generate Paging button method
  31. ViewState["TotalRecord"] = totalRecord;
  32. ViewState["NoOfRecord"] = noOfRecord;
  33. }
  34. }

Step-8: Write below function for Genarate button for Custom Paging.

  1. private void AddpagingButton()
  2. {
  3. // this method for generate custom button for Custom paging in Gridview
  4. int totalRecord = 0;
  5. int noofRecord = 0;
  6. totalRecord = ViewState["TotalRecord"] != null ? (int)ViewState["TotalRecord"] : 0;
  7. noofRecord = ViewState["NoOfRecord"] != null ? (int)ViewState["NoOfRecord"] : 0;
  8. int pages = 0;
  9. if (totalRecord >0 && noofRecord > 0)
  10. {
  11. // Count no of pages
  12. pages = (totalRecord / noofRecord) + ((totalRecord % noofRecord) > 0 ? 1 : 0);
  13. for (int i = 0; i < pages; i++)
  14. {
  15. Button b = new Button();
  16. b.Text = (i + 1).ToString();
  17. b.CommandArgument = (i + 1).ToString();
  18. b.ID = "Button_" + (i + 1).ToString();
  19. b.Click += new EventHandler(this.b_click);
  20. Panel1.Controls.Add(b);
  21. }
  22. }
  23.  
  24. }
  25.  
  26. protected void b_click(object sender, EventArgs e)
  27. {
  28. // this is for Get data from Database on button (paging button) click
  29. string pageNo = ((Button)sender).CommandArgument;
  30. PopulateData(Convert.ToInt32(pageNo), 5);
  31. }
Step-9: Run Application.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...