Link to home
Start Free TrialLog in
Avatar of yizchaknaveh
yizchaknaveh

asked on

It takes two clicks for a button to update my datagrid

Hi,

I have a datagrid, consisting of both template and bound columns, that's manually populated at runtime.  I'm trying to add a button that'll sort and display the data in the grid.  The sort is done by re-submitting a query to SQL Server with "ORDER BY" appended.  

Afterwards, the "sort-by" column is stored in Session so that whenever Page_Load occurs, the data will be sorted correctly.

My problem is, it appears to take two clicks of the button to sort the data grid.  In other words, the Button1_Click function fails to sort the grid (so Page_Load has to do it).

I tried stepping through the code, and whenever I press the button, it first runs Page_Load, and only THEN Button1_Click (so that Session["sort-by"] is set correctly only the next time Page_Load is run).

Please help! (Code below)

private void Page_Load(object sender, System.EventArgs e)
{                  
  if (!IsPostBack)
  {
    GetSource2();
    BindGrid();
  }
  else
  {
    if(Session["sort-by"] != null)
    sqlSelectCommand1.CommandText += " ORDER BY " + Session["sort-by"];
    GetSource2();
    BindGrid();
   }
}

void BindGrid()
{
  // Set the data source and bind to the Data Grid control.
  DataGrid1.DataSource = equipmentDataSet;
  DataGrid1.DataBind();
}

void GetSource2()
{
  EquipmentSqlConn.Open();
  sqlDataAdapter1.Fill(equipmentDataSet);
  EquipmentSqlConn.Close();
}

private void Button1_Click(object sender, System.EventArgs e)
{
  Session["sort-by"] = " IP";
  sqlSelectCommand1.CommandText += " ORDER BY IP";
  GetSource2();
  BindGrid();
}


ASKER CERTIFIED SOLUTION
Avatar of daffodils
daffodils

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yizchaknaveh
yizchaknaveh

ASKER

Thanks for your response.

I  tried this method to begin with, but it didn't work, since apparently template columns (which my datagrid includes) aren't sorted correctly using simple DataViews.

Hence the code you gave me doesn't work.

That's why I decided to resubmit the query to the server (which works fine).  Is there any way to make my program work using this method? (or another?)

"I  tried this method to begin with, but it didn't work, since apparently template columns (which my datagrid includes) aren't sorted correctly using simple DataViews."

A DataView sorts rows of a DataTable, which are then bound to your grid. It doesn't "sort" datagrid columns per se (if you're seeing something goofy, it's probably in how the binding is handled).

The DataView approach should work and is preferable, though it's typically done in the SortCommand event handler of the grid.

If interested, this may help:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatagridclasssortcommandtopic.asp

Thanks for your comment, also.

Everything else works correctly (e.g., updates, views) so I don't know why the binding would be goofy.  But still, how can I check that?

From what I read on various sites, template columns can cause problems when you sort a datagrid (or rather its underlying representation).

I know about SortCommand, but for now I'm just trying to get the damned thing to work first :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good point there boulder_bum... I didn't see that myself.
Yes that.. "else  {   if(Session["sort-by"] != null) }" block.. needs to go.

Here's some sample code from MSDN using Sort on DataGrid (with Template Columns).. you CAN use them together.

.NET Samples - ASP.NET Server Control Reference
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpqstart/html/cpsmpnetsamples-aspnetservercontrolreference.asp

Click on the link for "DataGrid9".. here is the source code for it...
http://samples.gotdotnet.com/quickstart/util/srcview.aspx?path=%2fquickstart%2faspplus%2fsamples%2fwebforms%2fdata%2fdatagrid9.src
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"Good point there boulder_bum... I didn't see that myself.
Yes that.. "else  {   if(Session["sort-by"] != null) }" block.. needs to go."

Did that clear things up? I'm crossing my fingers for ya.
Thank you kindly to everyone who responded!  

After attempting these solutions with no success, I realized, as boulder_bum suggested, that something else in my code was probably screwy (I tried to sort a datagrid on a fresh ASP.NET project and it worked fine).  So I just started with sample code from MSDN and gradually moved parts of my code to the new project.  Now I can sort the datagrid is sorted in just one click. ;)

I have a much better idea now of what goes on behind the scenes.  Thanks for clearing up my misapprehensions about the ASP.NET event model!

I've split the points between all of you.  I hope that's OK -- it's my first question on experts-exchange, and I'm not familiar with the rules yet...