Datagrid Add new Row problkem

V

Vishal

Hello,

I have a datagrid, which creates the columns
dynamically based on the columns in the dataset. Now I
need to add an additional row for each exisiting row AND
hide that row, by default. This row will contain my
usercontrol, which I will add also dynamically.
Unfortunaly I am not able to do so. I tried two options:

1.) I added the rows to the dataset and bound the dataset
to the datagrid.

- The problem however is, that I am not able to set the
span of that row somehow. I set it, but it doesnt take it.

- The second problem is that I am not able to hide it. I
dont see any attributes property for the DataRow. Here is
the code:

...begin loop through the rows
Dim NestedGridRow As DataRow = ds.Tables(0).NewRow
Dim NestedCell As New TableCell
NestedCell.ColumnSpan = 3 'ds.Tables(0).Columns.Count
ds.Tables(0).Rows.InsertAt(NestedGridRow, nRowPosition)
nRowPosition = nRowPosition + 2
...end loop through the rows

2.) I tried to add the columns direclty to the datagrid,
via the following code:

...begin loop through the rows
Dim dgItem As DataGridItem
Dim dgCell As TableCell
dgItem = New DataGridItem(0, 0, ListItemType.Item)
dgCell = New TableCell
dgCell.ColumnSpan = AADataGridStores.Columns.Count
dgItem.Cells.Add(dgCell)
dgCell.Text = "Testing something..."
Dim t As Table = AADataGridStores.Controls(0)
t.Rows.AddAt(nRowPosition, dgItem)
nRowPosition = nRowPosition + 2
...end loop through the rows

But this doesnt work too and it gives the error:
Specified argument was out of the range of valid values.
Parameter name: index

Now how I am supposed to add a new row which I can hide?
Manually, I would place the row into a placeholder and set
the placeholder property either to true/false. But I am
not getting how to do that in my situation. Can somebody
please help me with this one?

Thanks
 
E

Elton Wang

Hi Vishal,

I think basically your code of second option is OK. But,
there two things you should take into consideration:

1. Where to run the code? It should be after datagrid data
binding. It may be in PreRender.

2. Loop scope. Depending on different setting, you may
have or not Pager, Header, Footer. If you have any of
them, you should count them and add rows beyond them.

HTH

Elton Wang
(e-mail address removed)
 
V

Vishal

Hi Elton,

But i got the error as provided in the first message. I
am adding the code right now in Init. Do you think thats
the problem? How should I hide the row? Basically, the
datagrid will contain a button in each row, which will
lets me hide or show the nested row. As said I would did
that with a placeholder, but dunno, how to do it in my
current situation. Any help is greatly appreciated.

Thanks
 
E

Elton Wang

Hi Vishal,

As I mentioned, your logic is OK. But there are maybe two
things (maybe one of them maybe both of them) that cause
the trouble. One maybe is in wrong place to run your logic
(you can move code to Datagrid_PreRender). Another one is
from and to which exact row position you add those new
rows, I mean in

NRowPosition = ?startPositin
While (condition ?)

Currently you are using
dgCell.Text = "Testing something..."

You might use
dgCell.Controls.Add(yourcontrol)
in real situation.

HTH

Elton
 
V

Vishal

Hey Elton,

thanks for responding. I will try to move the code but
what about hiding the row? How should I do that?
 
V

Vishal

Is OnPreRender and DataGrid_PreRender the same? I have
currently defined it in page like this:

Protected Overloads Sub OnPreRender()
....
end sub

Is that ok?
 
V

Vishal

I added the code to the PreRender and now my complete dg
doesnt show up...

Private Sub AADataGridStores_PreRender(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
AADataGridStores.PreRender
CreateColumns()
End Sub
 
E

Elton Wang

You are right. It seems the viewstate can't keep added
rows, even add them very early (Page_Load, right after
data binding). So once postback, it shows trouble. But it
can't be done befor data binding. Currently I don't know
how to figure it out. If you find solution, please share
your idea.

Elton
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,138
Latest member
NevilleLam
Top