?
Solved

DataGrid View Problem - C#/ASP.Net

Posted on 2006-06-07
7
Medium Priority
?
361 Views
Last Modified: 2011-10-03
Is there any solution to this DataGrid problem, whenever the below code runs datagrid disappear from browser and reappear if we press Refresh button.

private void dgResult_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
            dgResult.CurrentPageIndex = e.NewPageIndex;
            dgResult.DataBind();
}
 
0
Comment
Question by:punjwani
  • 4
  • 3
7 Comments
 
LVL 25

Expert Comment

by:dstanley9
ID: 16855036
What do you mean " disappear from browser"?  You will have to do a post-back (which will re-load the page) to change pages - this is not done client-side.
0
 
LVL 1

Author Comment

by:punjwani
ID: 16855196
dstanley9, I have a very limited knowledge about this, can you tell me how and where to use postback, because I tried this and still giving me same problem.

private void dgResult4_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
            {
                  if (IsPostBack)
                  {
                        dgResult4.CurrentPageIndex = e.NewPageIndex;
                        dgResult4.DataBind();
                  }
            }
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16855281
postback just means the request is sent back to the server and the page is re-loaded.  The page does not change within the browser.  I assume that;s what you meant by "disappear from browser"...

what is in your Page_Load method?  If you call DataBind() again you will need to have the data source again (either cached or get if from the original source).
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:punjwani
ID: 16855511
There is nothing in page load method, and I am using binding() in pagechangeindex method.

private void Page_Load(object sender, System.EventArgs e)
      {
      if (!IsPostBack)
      {
            TextBox4.Text = DateTime.Now.ToString();
            TextBox1.Text="1";
            TextBox2.Text="1006";
            TextBox3.Text="0";
      }
     }

private void Button1_Click(object sender, System.EventArgs e)
    {
         try
      {
                    SqlConnection Cn = new SqlConnection();
          Cn.ConnectionString = @"here is my connection string";
                    sqlStatement4 = "MySQL Statement ";
                    SqlDataAdapter da4 = new SqlDataAdapter(sqlStatement4, Cn);
          da4.Fill(ds,"Query5");
          dgResult4.DataSource = ds.Tables["Query5"];
          dgResult4.Caption = "Rate Sheet";
          dgResult4.DataBind();                        
      }
         catch (Exception ex)
      {
            Label5.Visible=true;
            Label5.Text = ex.Message ;
      }
}

private void dgResult4_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
      dgResult4.CurrentPageIndex = e.NewPageIndex;
      dgResult4.DataBind();
}

This is complete page, I deleted unnecessary information.
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 2000 total points
ID: 16855640
OK I see what's happening.  I assumed you were loading data when the page was loaded.  What is happening is when the pageindexchanged event is fired, the grid is re-bound, but the data source is not set (since ASP.NET is stateless, the datasource is not kept within the datagrid).  So you are losing all of your data.

There are a couple of ways to handle this:

1) Save the DataSet in the session:


private void Button1_Click(object sender, System.EventArgs e)
    {
         try
     {
            ...
         dgResult4.DataSource = ds.Tables["Query5"];
         dgResult4.DataBind();    

         // Save the dataset in the session
         Session["Query5"] = ds.Tables["Query5"];              
     }
         catch (Exception ex)
     {
           Label5.Visible=true;
           Label5.Text = ex.Message ;
     }
}

private void dgResult4_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
     // Get the data back from te session
     dgResult4.DataSource = Session["Query5"];
     dgResult4.CurrentPageIndex = e.NewPageIndex;
     dgResult4.DataBind();
}

2) Put the data retrieval code into a separate function, and go back to the database to get the latest data :

private void Button1_Click(object sender, System.EventArgs e)
    {
         try
     {
         DataSet ds = GetData();
         dgResult4.DataSource = ds.Tables["Query5"];
         dgResult4.Caption = "Rate Sheet";
         dgResult4.DataBind();                    
     }
         catch (Exception ex)
     {
           Label5.Visible=true;
           Label5.Text = ex.Message ;
     }
}
private void dgResult4_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
     DataSet ds = GetData();
     dgResult4.DataSource = ds.Tables["Query5"];
     dgResult4.CurrentPageIndex = e.NewPageIndex;
     dgResult4.DataBind();
}


privte DataSet GetData()
{
         DataSet ds = new DataSet();
         SqlConnection Cn = new SqlConnection();
         Cn.ConnectionString = @"here is my connection string";
         sqlStatement4 = "MySQL Statement ";
         SqlDataAdapter da4 = new SqlDataAdapter(sqlStatement4, Cn);
         da4.Fill(ds,"Query5");
         return ds;
}

Which method you choose depends on things like 1) how often the data changes, 2) how much data is reterieved, and 3) how fast you want the page to be.  Using the session is faster, but there's a chance of having stale data.
0
 
LVL 1

Author Comment

by:punjwani
ID: 16855716
Great, Thanks for all your help it worked for me.

Thanks allot.
0
 
LVL 1

Author Comment

by:punjwani
ID: 16896676
dstanley9, if possible for you please answer my next question about DataGrid Sorting, I post that question seprately.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question