Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
I am binding the gridview with different conditions. Page numbers are showing on grid view but on clicking nothing happens.
How to define logic in GridView1_PageIndexChanging event?

My code is as follow:

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.SqlClient;
using System.Data;

public partial class Main : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection();
    SqlCommand com = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();


    protected void Page_Load(object sender, EventArgs e)
    {
        con.ConnectionString = "server = 10.139.244.22; initial catalog = Connectivity_Status; user id = sa; password = foRest4321";
        con.Open();
        if (!IsPostBack)
        {

            FillCircle(ddlCircle);

        }

    }

    protected void ddlCircle_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlCircle.SelectedValue == "1")
            BindGridAll();
        else
            GridView1.DataSource = ds.Tables;
        GridView1.DataBind();
        FillDivision(ddlCircle.SelectedValue.ToString());
    }

    protected void ddlDivision_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlDivision.SelectedValue == "1")
            BindGridDivisionAll(ddlCircle.SelectedValue.ToString());
        else
            GridView1.DataSource = ds.Tables;
        GridView1.DataBind();
        FillRange(ddlDivision.SelectedValue.ToString());
    }

    protected void ddlRange_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlRange.SelectedValue == "1")
            BindGridRangeAll(ddlDivision.SelectedValue.ToString());
        else
            GridView1.DataSource = ds.Tables;
        GridView1.DataBind();
        BindGrid(ddlRange.SelectedValue.ToString());
    }

    protected void FillCircle(DropDownList ddlCircle)
    {
        try
        {
            com.CommandText = "SELECT [CIRCLE_ID],[CIRCLE_ENAME] FROM [Connectivity_Status].[dbo].[mCircle] ";
            com.Connection = con;
            da.SelectCommand = com;
            da.Fill(ds, "C");


            ddlCircle.DataSource = ds.Tables["C"];
            ddlCircle.DataTextField = "CIRCLE_ENAME";
            ddlCircle.DataValueField = "CIRCLE_ID";
            ddlCircle.DataBind();

            ListItem lstTemp6 = new ListItem("--Select--", "0");
            ddlCircle.Items.Insert(0, lstTemp6);
            ListItem lstTemp7 = new ListItem("All", "1");
            ddlCircle.Items.Insert(1, lstTemp7);

            ddlCircle.SelectedIndex = 0;
            da.Dispose();
        }
        catch (Exception ex)
        {
        }
        finally
        {
        }
    }



    protected void FillDivision(string value)
    {
        try
        {
            com.CommandText = "SELECT [CIRCLE_ID],[DIV_ID],[DIV_ENAME] FROM [Connectivity_Status].[dbo].[mDIVISION] where CIRCLE_ID = " + value + " ";
            com.Connection = con;
            da.SelectCommand = com;
            da.Fill(ds, "C");


            ddlDivision.DataSource = ds.Tables["C"];
            ddlDivision.DataTextField = "DIV_ENAME";
            ddlDivision.DataValueField = "DIV_ID";
            ddlDivision.DataBind();

            ListItem lstTemp8 = new ListItem("--Select--", "0");
            ddlDivision.Items.Insert(0, lstTemp8);
            ListItem lstTemp9 = new ListItem("All", "1");
            ddlDivision.Items.Insert(1, lstTemp9);
            ddlDivision.SelectedIndex = 0;
            da.Dispose();
        }
        catch (Exception ex)
        {
        }
        finally
        {
        }
    }


    protected void FillRange(string value)
    {
        try
        {
            com.CommandText = "SELECT  [RANGE_ID],[Div_id],[RANGE_ENAME] FROM [Connectivity_Status].[dbo].[mRANGE] where Div_id = " + value + " ";
            com.Connection = con;
            da.SelectCommand = com;
            da.Fill(ds, "C");



            ddlRange.DataSource = ds.Tables["C"];
            ddlRange.DataTextField = "RANGE_ENAME";
            ddlRange.DataValueField = "RANGE_ID";
            ddlRange.DataBind();

            ListItem lstTemp10 = new ListItem("--Select--", "0");
            ddlRange.Items.Insert(0, lstTemp10);
            ListItem lstTemp11 = new ListItem("All", "1");
            ddlRange.Items.Insert(1, lstTemp11);
            ddlRange.SelectedIndex = 0;
            da.Dispose();
        }
        catch (Exception ex)
        {
        }
        finally
        {
        }
    }
    protected void BindGrid(string value)
    {
        try
        {
            com.CommandText = ("select I_Info.RANGE_ID, mRANGE.RANGE_ENAME,I_Info.INTERNET_TYPE,I_Info.INTERNET_STATUS from Connectivity_Status.dbo.I_Info,Connectivity_Status.dbo.mRANGE where Connectivity_Status.dbo.I_Info.RANGE_ID=Connectivity_Status.dbo.mRANGE.RANGE_ID And Connectivity_Status.dbo.I_Info.RANGE_ID= " + value + "  ORDER BY RANGE_ENAME");
            com.Connection = con;
            da.SelectCommand = com;
            da.Fill(ds, "R");

            GridView1.DataSource = ds.Tables["R"];
            GridView1.DataBind();
            da.Dispose();

        }
        catch (Exception ex)
        {
        }
        finally
        {
        }

    }
    protected void BindGridAll()
    {
        try
        {
            com.CommandText = ("select I_Info.RANGE_ID, mRANGE.RANGE_ENAME,I_Info.INTERNET_TYPE,I_Info.INTERNET_STATUS from Connectivity_Status.dbo.I_Info,Connectivity_Status.dbo.mRANGE where Connectivity_Status.dbo.I_Info.RANGE_ID=Connectivity_Status.dbo.mRANGE.RANGE_ID ORDER BY I_Info.CIRCLE_ID,I_Info.DIV_ID,I_Info.RANGE_ID");
            com.Connection = con;
            da.SelectCommand = com;
            da.Fill(ds, "R");

            GridView1.DataSource = ds.Tables["R"];
            GridView1.DataBind();
            da.Dispose();
        }
        catch (Exception ex)
        {
        }
        finally
        {
        }

    }

    protected void BindGridDivisionAll(string value)
    {
        try
        {
            com.CommandText = ("select distinct I_Info.RANGE_ID, mRANGE.RANGE_ENAME,I_Info.INTERNET_TYPE,I_Info.INTERNET_STATUS from Connectivity_Status.dbo.I_Info,Connectivity_Status.dbo.mRANGE,Connectivity_Status.dbo.mDIVISION where Connectivity_Status.dbo.I_Info.RANGE_ID=Connectivity_Status.dbo.mRANGE.RANGE_ID and Connectivity_Status.dbo.mRANGE.DIV_ID = Connectivity_Status.dbo.mDIVISION.DIV_ID and Connectivity_Status.dbo.I_Info.CIRCLE_ID = " + value + " ORDER BY RANGE_ENAME");
            com.Connection = con;
            da.SelectCommand = com;
            da.Fill(ds, "R");

            GridView1.DataSource = ds.Tables["R"];
            GridView1.DataBind();
            da.Dispose();

        }
        catch (Exception ex)
        {
        }
        finally
        {
        }

    }

    protected void BindGridRangeAll(string value)
    {
        try
        {
            com.CommandText = ("select distinct I_Info.RANGE_ID, mRANGE.RANGE_ENAME,I_Info.INTERNET_TYPE,I_Info.INTERNET_STATUS from Connectivity_Status.dbo.I_Info,Connectivity_Status.dbo.mRANGE,Connectivity_Status.dbo.mDIVISION where Connectivity_Status.dbo.I_Info.RANGE_ID=Connectivity_Status.dbo.mRANGE.RANGE_ID and Connectivity_Status.dbo.mRANGE.Div_id  = Connectivity_Status.dbo.mDIVISION.DIV_ID and Connectivity_Status.dbo.I_Info.DIV_ID = " + value + " ORDER BY RANGE_ENAME");
            com.Connection = con;
            da.SelectCommand = com;
            da.Fill(ds, "R");

            GridView1.DataSource = ds.Tables["R"];
            GridView1.DataBind();
            da.Dispose();
        }
        catch (Exception ex)
        {
        }
        finally
        {
        }
    }
 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        //BindGrid(Session["Language"].ToString());
      BindGridAll();
        //BindGridDivisionAll(Session["Language"].ToString());
        //BindGridRangeAll(Session["Language"].ToString());
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (((Label)e.Row.FindControl("lblINTERNET_STATUS")).Text != " ")     
                {
                    if (((Label)e.Row.FindControl("lblINTERNET_STATUS")).Text == "not working")
                    {
                        e.Row.Cells[3].BackColor = System.Drawing.Color.Crimson;
                    }
                    else
                    {
                        e.Row.Cells[3].BackColor = System.Drawing.Color.Green;
                    }
                }
            }
        }
    }
}
Posted
Updated 6-Aug-13 2:09am
v4
Comments
Ankur\m/ 6-Aug-13 7:37am    
This is a code dump. Please see the guidelines before you ask a question.
Put a debugger and seed what's wrong with the code. See if the code is executed or not. If yes, is there an exception or the logic is failing somewhere. When you have the specifics and are still not able to solve the issue, post that here.

Use 'Improve Question' to update the question with just the necessary details.
Harshil_Raval 6-Aug-13 7:50am    
Agree with you. And other things is what you mean by gridview page number? Are you talking about paging?
Thanks7872 6-Aug-13 8:06am    
Post only the code where you are binding the gridview and the page index changing method. Remove unnecessary code in order to make some one understand the code and rectify it.
Thanks7872 6-Aug-13 8:08am    
One thing i noted here is you used try catch just to hide errors. If you remove that,i think error will be more self explanatory.
Ankur\m/ 6-Aug-13 8:10am    
@RelicV - When you are editing a question, be sure it edit it correctly. See the preview of your edit and if you are not getting it correctly, please leave it.

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