开发者

Gridview with DDl selected value

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 = n开发者_开发知识库ew 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();
    }
}


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"


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜