tags:

views:

43

answers:

2

I had gridview which in load it will get data from database .And I added option for user to filter this grid view by DDl I did my code and the grid get data when load but when I selected DDl it didnot get any data and I made break point I noticed that Gridview1.Databind() hadnot any action on grid.So please any one help me

protected void Page_Load(object sender, EventArgs e)
{
    DataTable DT = new DataTable();

              if (DDlCity.SelectedIndex<0)
        {
            using (SqlConnection con = Connection.GetConnection())
            {
                SqlCommand Com = new SqlCommand("GetDealers", con);
                Com.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter DA = new SqlDataAdapter(Com);
                DA.Fill(DT);
                GridView1.DataSource = DT;
                GridView1.DataBind();
            }

        }


    }

protected void DDlCity_SelectedIndexChanged(object sender, EventArgs e)
{
    DataTable DT = new DataTable();
    using (SqlConnection con = Connection.GetConnection())
    {
        SqlCommand Com = new SqlCommand("GetDealersByArea", con);
        Com.CommandType = CommandType.StoredProcedure;
        Com.Parameters.Add(Parameter.NewInt("@DEALERAREA_ID", DDlCity.SelectedValue));
        SqlDataAdapter DA = new SqlDataAdapter(Com);
        DA.Fill(DT);
        GridView1.DataSource = DT;
        GridView1.DataBind();
    }
}
A: 

In your post back you can put the binding code in condition

if (!IsPostBack){
 // Bind grid here looking for or used call to function something like BindGrid()
}

and in BindGrid() function you can write binding code for grid view.

and on ddl selected index changed event you can again call the BindGrid() method to bind again accordingly.

also check that in your dropdownlist you have EnablePostBack - true.

J Sinh
Yeah, that's pretty much what I was going to say...
PhilPursglove
thanks for the appreciation.
J Sinh
I did that you sent
KareemSaad
this function protected DataTable GetGrid() { using (SqlConnection con = Connection.GetConnection()) { SqlCommand Com = new SqlCommand("GetDealersByArea", con); Com.CommandType = CommandType.StoredProcedure; Com.Parameters.Add(Parameter.NewInt("@DEALERAREA_ID", DDlCity.SelectedValue)); SqlDataAdapter DA = new SqlDataAdapter(Com); DA.Fill(DT); GridView1.DataSource = DT; GridView1.DataBind(); return DT; } }
KareemSaad
protected void DDlCity_SelectedIndexChanged(object sender, EventArgs e) { GetGrid(); }
KareemSaad
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetGrid(); GridView1.DataBind();}}
KareemSaad
but error apear Failed to convert parameter value from a String to a Int32 in function
KareemSaad
A: 

i suppose you got confused on what i said...no worries here is a working example of you give example code.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGridFunction();
    }
}

private void BindGridFunction()
{
    try
    {
        DataTable DT = new DataTable();
        using (SqlConnection con = Connection.GetConnection())
        {
            if(DDlCity.SelectedIndex <0)
            {
                SqlCommand Com = new SqlCommand("GetDealers", con);
                Com.CommandType = CommandType.StoredProcedure;
            }
            else
            {
                SqlCommand Com = new SqlCommand("GetDealersByArea", con);
                Com.CommandType = CommandType.StoredProcedure;
                Com.Parameters.Add(Parameter.NewInt("@DEALERAREA_ID", DDlCity.SelectedItem.Value));
            }

            SqlDataAdapter DA = new SqlDataAdapter(Com);
            DA.Fill(DT);
            GridView1.DataSource = DT;
            GridView1.DataBind();
        }
    }
    catch(Exception ex)
    {
        DT = null; // etc...etc.. clear objects created
    }

}


protected void DDlCity_SelectedIndexChanged(object sender, EventArgs e)
{
    BindGridFunction();
}

I hope you get what i was trying to say. You can change the code according to you need.

Not tested yet but m sure will work.

Note : i woud suggest to use "DDlCity.SelectedItem.Value" instead of " DDlCity.SelectedValue"

J Sinh
I did that you sent but DataBind() it did not change the contents of grid when I selected DDl
KareemSaad
i suppose u need to check if the sp is returning the correct result you need to bind >>>> Com.Parameters.Add(Parameter.NewInt("@DEALERAREA_ID", DDlCity.SelectedItem.Value)); can u get the code of sp u are using "GetDealersByArea" see if there is some problem with that....
J Sinh
Create proc [dbo].[GetDealersByArea](@DEALERAREA_ID Int)ASSELECT Dealers.DEALER_ID, Dealers.NAME_ENG, DealerArea.AREA_ID, DealerArea.AREA_ENG, DealerArea.AREA_ARAFROM Dealers INNER JOIN DealerLocation ON Dealers.LOC_ID = DealerLocation.LOC_ID INNER JOIN DealerCategories ON Dealers.CAT_ID = DealerCategories.CAT_ID INNER JOIN DealerArea ON DealerLocation.AREA_ID = DealerArea.AREA_IDWhere DealerArea.AREA_ID=@DEALERAREA_ID
KareemSaad
this sp return DEALER_ID, NAME_ENG, AREA_ID, AREA_ENG AREA_ARA, the other sp (GetDealers) should be returning the same fields ??? other than that the problem might be u need to send the @DEALERAREA_ID value as int as DDlCity.SelectedItem.Value gives string..... other than that check the code where you have declared the dropdownlist - its items should be having value in them.
J Sinh