Click here to Skip to main content
15,923,222 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hello GoodMorning,
i am trying to display the number of rows in a gridview on dropdownlist selection.But the problem i
am facing is that on page load for the first time its showing relevent result but on selecting some other value from the DDL Grid view is not iterating.
Please help me with this..??
here is the code:
in .aspx i have taken 1 DDL and 1 Grid view
in code behing:
C#
public partial class GridViewDemo : System.Web.UI.Page
{
    private void BindGridView1()
    {
        try
        {
            this.GridView1.DataSource = ProjectManager.GetHoodListing();
            this.GridView1.DataBind();
        }
        catch (Exception ex)
        { throw ex; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                BindGridView1();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView1.PageSize = Convert.ToInt32(DropDownList1.SelectedValue);
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

            }
        }
        catch (Exception ex)
        { throw ex; }
    }
**************************************************
// project manager class

 public static DataTable GetHoodListing()
    {
        DataAccess objDA = new DataAccess();
        DataTable objDT = new DataTable();
        string strSql = "";
        try
        {
            strSql = string.Format("SELECT HLID, HLSeries, HLStyle, HLCanopProfile FROM HoodListing");
            objDT = objDA.ExecuteDataTable(strSql);
        }
        catch (SqlException sqlex)
        { throw sqlex; }
        catch (Exception ex)
        { throw ex; }

        return objDT;
    }
Posted
Updated 30-May-12 19:00pm
v2

include BindGridView1(); in GridView1_PageIndexChanging event.
u will get it.
 
Share this answer
 
Comments
saurabh kumar mahto 31-May-12 1:36am    
thanks a lot Bhargava..for your great support..
its working.
saurabh kumar mahto 31-May-12 2:40am    
Hi,
everything is working fine but a small issue here is that when i click any umber in DDL its not iterating the Gridview on postback,and i have to click on page index for chage reflection.?
i want it to reflect on immediate postback..
You forgot to redefine DataSource in your PageIndexChanging event.

Modify your code to:
C#
// Pagesize set at the time of binding
private void BindGridView1()
{
        try
        {
            this.GridView1.DataSource = ProjectManager.GetHoodListing();
            this.GridView1.DataBind();

            if(Convert.ToInt32(DropDownList1.SelectedValue) != null)
              GridView1.PageSize = Convert.ToInt32(DropDownList1.SelectedValue);
        }
        catch (Exception ex)
        { throw ex; }
}

// page changed and grid rebind to reflect change
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
        GridView1.PageIndex = e.NewPageIndex;
        BindGridView1();
}

//To set grid with defined page as per set
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
       BindGridView1();
}
 
Share this answer
 
Comments
saurabh kumar mahto 31-May-12 1:39am    
thanks sandeep for your response..its working now.
Sandeep Mewara 31-May-12 1:43am    
Welcome.
saurabh kumar mahto 31-May-12 4:29am    
Hi,
everything is working fine but a small issue here is that when i click any umber in DDL its not iterating the Gridview on postback,and i have to click on page index for chage reflection.?
i want it to reflect on immediate postback..
Sandeep Mewara 31-May-12 5:20am    
What I gave is based on dropdown selection, grid is re-rendered with page 1 showing and selected page size.

If you want to move to page 2, call pagechanged index.
Member 11503344 12-Mar-15 2:14am    
Thanks for the help,it works great.

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