Maintaining Datagrid State on manipulated columns

  • Thread starter Thread starter Jason
  • Start date Start date
J

Jason

I have a datagrid inside a user control. I use the itemcreated event
to manipulate column values. I then add those values up and place the
total in the footer of the datagrid. I have wrapped the function used
to obtain the data for this datagrid in a page.ispostback=false
condition. Everything displays accurately when an event(in the user
control) posts the page back to the server, but if a control outside
the user control posts the page back to the server my manipulated data
is missing (column data and summary info in the footer). The only
solution I have come up with is to remove the ispostback condition,
but then I take a performance hit because I have to go back to the
database on every post back. This really slows the page down. Does
anyone have any ideas??

Thank you,
Jason
 
Try to set the column data and summary info in the "ItemDataBound"
instead, and don't handle the "ItemCreated" event.

The "ItemCreated" event fires every time the page is loaded.

However, because on a postback you don't rebind to your datasource,
the "ItemCreated" event is unable to retrieve the data to calculate
the data you need, and reset the column data and summary info to
blank.

If you don't handle the "ItemCreated" event, ASP.NET will just restore
the data to the datagrid from the viewstate, and thus preserve your
column data and summary info.

Tommy,
 
Jason said:
Does anyone have any ideas??

In addition to Tommy's suggestions, you might consider skipping the
ItemDataBound event altogether if you're using a DataSet or DataTable as
the data source for your grid. The DataTable class has a Compute method
which will execute aggregate functions against rows that match the
specified filter:

dataTable.Compute("Sum(<column>)", "Qty > 0");

If there's some handling you have to perform between columns first, you
can always add a calculated column to your table like so:

DataColumn column = dataTable.Columns.Add("ExtdCost", typeof(double));
column.Expression = "Qty * Cost";

Once added, you can then sum that column as shown above. This is a lot
more efficient than ItemDataBound.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Back
Top