4 Replies - 13127 Views - Last Post: 19 March 2012 - 06:42 AM

#1 vikramsinh_shinde  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 22-February 12

Display gridview after button click

Posted 27 February 2012 - 02:55 AM

I have a stored procedure. I wants to develop a very simple asp.net page in which, there will be only button called "Show Data". I want to bind my stored procedure to this button and after clicking this button the page should show gridview data.

And if possible I want to implement another button named "Hide Data" which will hide same gridview after clicking it.
Is This A Good Question/Topic? 0
  • +

Replies To: Display gridview after button click

#2 strawhat89  Icon User is offline

  • The Watcher Outside Your Window


Reputation: 247
  • View blog
  • Posts: 1,795
  • Joined: 11-July 11

Re: Display gridview after button click

Posted 27 February 2012 - 03:05 AM

in the onclick event handler method of the show data button, you will need to add something like

DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand("StoredProcedureName", connectionName);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ParameterName", value);
...
SqlDataAdapter dataAdapt = new SqlDataAdapter(cmd);
dataAdapt.Fill(ds);



i guess that should be enough for you to figure it out..
Was This Post Helpful? 1
  • +
  • -

#3 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Display gridview after button click

Posted 15 March 2012 - 01:06 PM

Once you have filled your DataSet, set the GridView.DataSource property to the table in the DataSet that you want to display and then call the GridView.DataBind() method to bind to the data (and display it).

myGridView.DataSource = ds.Tables[0];
myGridView.DataBind();



I would call the DataBind method in the Page PreRender event. Avoid calling it in the PageLoad event or you will run into problems editing the values in the GridView.

-Frinny

This post has been edited by Frinavale: 15 March 2012 - 01:07 PM

Was This Post Helpful? 1
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Display gridview after button click

Posted 18 March 2012 - 05:58 PM

Another good idea when databinding like this is to store the DataSet or DataTable using a ViewState backed property. This will allow you to rebind the GridView on postback's without making additional calls to your database.

public DataSet DataSource
{
  get
  {
    object temp = ViewState["DataSource"];
    return temp == null ? null : (DataSet)temp;
  }
  set
  {
    ViewState["DataSource"] = value;
  }
}



In your Page_PreRender event you just check if DataSource is null, if not then use it to bind your GridView.
Was This Post Helpful? 1
  • +
  • -

#5 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Display gridview after button click

Posted 19 March 2012 - 06:42 AM

You could also use Control State, or ASP.NET Cache, or Session to store your results like Nakor is suggesting. This reduces the need to go to retrieve data from your database all the time. You just need to implement something that will refresh the data at an appropriate time in your application.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1