Click here to Skip to main content
15,914,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

page numbers for grid view control using asp.net

here we use two drop down list boxes and one grid view

and once select cars in dropdownlist1 display all cars details in grid view and drop down list2 also select high to low and low to high select any one will display records in gridview control displaying output properly but page numbers is not working properly

here below design and code please check reply this way correct or not and any another way please reply me


design:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;

public partial class Cars : System.Web.UI.Page
{
    SqlCommand cmd = new SqlCommand();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
            BindGridView();
            PriceList();
        }
    }
    private void LoadData()
    {
        string constr = "Data Source=pmnb3-PC;Database=Save;user id=sa;password=123;";
        string query = "SELECT ID, Name, Address, Email, Mobile, Category,Price FROM PostTable";

        SqlDataAdapter da = new SqlDataAdapter(query, constr);
        DataTable table = new DataTable();
        da.Fill(table);
        GridView1.DataSource = table;
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        LoadData();
        BindGridView();
        PriceList();
    }
    protected void BindGridView()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        if (DropDownList1.SelectedItem.Text == "Cars")
        {
            cmd = new SqlCommand("Select * From PostTable where Category='Cars'", con);
        }
        else if (DropDownList1.SelectedItem.Text == "Mobiles")
        {
            cmd = new SqlCommand("select * from PostTable where Category='Mobiles'", con);
        }
        else if (DropDownList1.SelectedItem.Text == "--Select--")
        {
            cmd = new SqlCommand("select * from PostTable", con);
        }

        SqlDataAdapter adap = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adap.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGridView();
    }
    protected void PriceList()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        if (DropDownList2.SelectedItem.Text == "Price: High To Low")
        {
            cmd = new SqlCommand("Select * From PostTable order by price desc", con);
        }
        else if (DropDownList2.SelectedItem.Text == "Price: Low To High")
        {
            cmd = new SqlCommand("select * from PostTable order by price asc", con);
        }
        else if (DropDownList2.SelectedItem.Text == "Most Recently Ads")
        {
            cmd = new SqlCommand("select * from PostTable order by id desc", con);
        }

        SqlDataAdapter adap = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adap.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        PriceList();
    }
}
Posted
Updated 24-Sep-14 21:52pm
v2

1 solution

Make sure you have set Gridview1.AllowPaging=True;
Then merge your LoadData(); BindGridView(); PriceList(); to One single function like

C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!Page.IsPostBack)
       {
          BindGridView();

       }
   }


   protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       GridView1.PageIndex = e.NewPageIndex;
       BindGridView();
   }

   protected void BindGridView()
   {
       string strCmd ="SELECT ID, Name, Address, Email, Mobile, Category,Price FROM PostTable WHERE (Category=@Category OR @Category IS NULL) ";

       if (DropDownList2.SelectedItem.Text == "Price: High To Low")
           strCmd=strCmd+"order by price desc";
       else if (DropDownList2.SelectedItem.Text == "Price: Low To High")
           strCmd=strCmd+"order by price asc";
       else
           strCmd=strCmd+"order by id desc";


       using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
       {
           using(SqlDataAdapter adap = new SqlDataAdapter(strCmd,con))
           {
                if (DropDownList1.SelectedItem.Text == "--Select--")
                     adap.SelectCommand.Parameters.Add("@Category",SqlDbType.NVarChar,50).Value=DBNull.Value;
                else if (DropDownList1.SelectedItem.Text == "Mobiles")
                     adap.SelectCommand.Parameters.Add("@Category",SqlDbType.NVarChar,50).Value=DropDownList1.SelectedItem.Text;

               using(DataTable dtbl =new DataTable())
               {
                   adap.Fill(dtbl);
                    GridView1.DataSource = dtbl;
                    GridView1.DataBind();
               }
           }
       }


   }
   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       BindGridView();
   }

   protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
   {
       BindGridView();
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900