adding rows

P

Perecli Manole

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
 
R

Raghavendra T V

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
 
P

Perecli Manole

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
 
A

Alvin Bruney [MVP]

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
 
P

Perecli Manole

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


Alvin Bruney said:
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
Perecli Manole said:
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
 
A

Alvin Bruney [MVP]

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
Perecli Manole said:
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


Alvin Bruney said:
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
Perecli Manole said:
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
 
P

Perecli Manole

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


Alvin Bruney said:
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
Perecli Manole said:
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


Alvin Bruney said:
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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top