When I add an extra header row with spanning, my row data disappears one at a time

S

ShamusFu

I added a second header row to my datagrid with spanned columns for
looks, but it seems when I post back from other buttons on the page,
the bound data in the rows disappears one at a time. The data is not
deleted, since when I edit the DG it all comes back, but it looks
ugly. You can press a submit button on the page for some other action
and the row data disappears one at a time. The rows are still there,
they just have blank cells.

Here is the code for adding another header row and spanning:


Dim dgitem As New DataGridItem(0, 0, ListItemType.Header)

Dim emptycell As New TableCell
emptycell.ColumnSpan = 2 'Set it to the colspan that you want
emptycell.Text = ""

Dim headcell As New TableCell
headcell.ColumnSpan = 3 'Set it to the colspan that you want
headcell.Text = "Head circ.(ofc)"
headcell.Font.Bold = True

Dim lenghtcell As New TableCell
lenghtcell.ColumnSpan = 3 'Set it to the colspan that you want
lenghtcell.Text = "Length/height"
lenghtcell.Font.Bold = True

Dim weightcell As New TableCell
weightcell.ColumnSpan = 3 'Set it to the colspan that you want
weightcell.Text = "Weight"
weightcell.Font.Bold = True

Dim bmicell As New TableCell
bmicell.ColumnSpan = 2 'Set it to the colspan that you want
bmicell.Text = "BMI"
bmicell.Font.Bold = True

Dim editcell As New TableCell
editcell.ColumnSpan = 2 'Set it to the colspan that you want
editcell.Text = ""
dgitem.Cells.Add(emptycell)
dgitem.Cells.Add(headcell)
dgitem.Cells.Add(lenghtcell)
dgitem.Cells.Add(weightcell)
dgitem.Cells.Add(bmicell)
dgitem.Cells.Add(editcell)
dgPatientData.Controls(0).Controls.AddAt(0, dgitem)
 
K

Kathleen Dollard

I suspect your problem is unrelated to the spanned columns. Can you remove
that and see if it works as you'd expect?

Kathleen
 
Joined
Jun 27, 2007
Messages
1
Reaction score
0
I have the EXACT same problem, but I know a little bit about what's causing it.

I'm very sure problem isn't related to the fact that we're using spanned columns, but that we're adding rows dynamically to the datagrid. When you postback, the server doesn't know that those extra rows exist due to the fact that they were added dynamically to the controls collection, hence that row's information is lost on postback (including the fact that it was a header row, it gets wiped blank and becomes the new first item row, pushing the last item row out of the bottom of the table).

I'm guessing that you're re-creating your dynamic header row on every postback? If you want to test out what I've said, only add your dynamic header row once (when the datagrid is initialised). On the first postback the new header row will disappear, becoming a new blank row just underneath the original header and pushing your last row of data out of the bottom of the table. Since your heading row hasn't been re-created this time, subsequent postbacks will not create any more blank rows.



Phew, after I've said all of that, the bad news is that I have no idea how to fix this problem other than not adding rows dynamically in the first place. In my case though, adding dynamic rows to the heading is unavoidable... If anybody else here has any ideas of how to stop the dynamic row's data from being lost on postback, please let us know!

EDIT: Well... I didn't check the date of the original message now, did I... Regardless, if anyone has a solution to this problem, I'd greatly appreciate it. I've been searching for a solution to this problem for days.

EDIT 2: Well what do you know, after two and a half days of non-stop searching I finally found the answer.

Rather than adding new header rows to the DataGrid's control collection, I'm overriding the DataGrid's header item rendering method, and injecting my own HTML code directly instead. This way, I need to rebind the DataGrid after each postback, but atleast I'm not losing data anymore.

See the following link for more information: http://www.codeproject.com/aspnet/MergeDatagridHeader.asp

I hope that this helps the next person who encounters the same problem as me.
 
Last edited:

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

Top