Click here to Skip to main content
15,921,276 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have connected GridView with a datasource. It works fine but it is displayed just at form load. I want to show it when user clicks the button. How to make it so?

Thanks
Posted

Any code example please?
protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            Bind();
        }
    }
private void Bind()
{
  string connectionString =    
  WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
  string selectSQL ="SELECT ProductID, ProductName, UnitPrice FROM Products";
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(selectSQL, con);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();

            adapter.Fill(ds, "Products");

            GridView1.DataSource = ds;
            GridView1.DataBind();

}
//I want to show it when user clicks the button
private void Button1_click(Object sender,EventArgs e)
{
    Bind();
}

source[^]
 
Share this answer
 
So, do you have any check for IsPostBack ? Bind the Data on Click of Button.
 
Share this answer
 
Comments
Furqan Sehgal 4-Sep-11 11:56am    
Any code example please?

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