all groups > asp.net datagrid control > august 2004 >
You're in the

asp.net datagrid control

group:

adding rows


adding rows Perecli Manole
8/26/2004 9:38:01 PM
asp.net datagrid control:
In my DataGrid I need to have two rows of data for every bound record. I
have successfully achieved this by adding datagrid items and cells through
code in the "DataGrid.ItemDataBound" event. The problem is that on post back
my added rows do not stay. Aparently one needs to recreate them each page
request. I have read several articles that suggest building up your grid
layout in the "DataGrid.ItemCreated" event which gets called every request.
So I put my row adding code there but then I realized that this event gets
called before the framework adds its default row in the grid which creates a
problem. I need my row to be added after the framework adds the default row
so that it can be placed below it. I can not place a row with an index after
a row that has not been created by the framework. So for now it seems like
the only solution is to bind the grid on every request which is a waste. Is
there another solution.

Perry

Re: adding rows Perecli Manole
8/27/2004 2:54:58 PM
The code that adds rows can not be in a generic function because the rows
need to be added in a special iterative function such as
"DataGrid.ItemDataBound" or "DataGrid.ItemCreated". These functions are
special in that they are called by the framework once per record and the
framework passes in the current DataItem and RowItem being proccessed. I can
not duplicate this behavior with a standard function call.

Perry

[quoted text, click to view]

Re: adding rows Raghavendra T V
8/27/2004 3:00:31 PM
Hi Perecli,

I guess if you could put your code which adds rows to datagrid in a function
and
call them the Datagrid events and you need to take care of postback by
checking for Page.IsPostBack

Thanks
Raghavendra

[quoted text, click to view]

Re: adding rows Alvin Bruney [MVP]
8/28/2004 3:05:47 PM
what you need to do is add the rows to the dataset instead. whenever you
bind, the extra rows are always there and you don't need to go thru the
hocus pocus of postback and viewstate hell

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
[quoted text, click to view]

Re: adding rows Perecli Manole
8/30/2004 5:16:25 PM
You missunderstand my requirement. The reason I need to add a row dynamicaly
to the grid is because my row table strucuture needs to be changed. For
every row in the database I need two rows in the grid. First row for each
record should have one column structure and shows half the data while the
second row has a different column structure and shows the other half of the
data. In edit mode I need to go back to a "signle row/single column" because
I show an entry form in it. I have achieved this by altering the grid's
structure in the DataBind event however I have the problem described in my
previous post.

Perry


[quoted text, click to view]

Re: adding rows Alvin Bruney [MVP]
8/31/2004 10:36:34 AM
you will need to bind on every request, or you can try putting the bind code
early in the pipeline so that it can participate in viewstate. you will have
to look at the pipeline events to find a suitable one (prerender comes to
mind but i'm not sure that it is early enough to participate in viewstate)

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
[quoted text, click to view]

Re: adding rows Perecli Manole
8/31/2004 4:13:17 PM
Even on load was not early enough to participate in viewstate. I have tried
this in the Initialize event and it works except the viewstate gets
confused. Apparently the columns and row modifications I am making to the
grid are offsetting the viewstate matching so I get same good results and
some garbled results. The framework dows not seem to save the viewstate of
the dynamic alterations to the grid but only of the datagrid as it thinks
was built. The trick is not to use the built in viewstate and manage it
yourself. What I ended up doing is saving the DataSet result in the view
state too that way even though I am binding on every postback, the net
result is still only one DB hit. Only one problem remains. The fields that
are shown in edit mode do not hold their values when a post is made that
shows a validation error. I need to somehow get the Forms values and
populate my controls with them as in the old ASP days. Any suggestions?

Thanks
Perry


[quoted text, click to view]

AddThis Social Bookmark Button