Link to home
Start Free TrialLog in
Avatar of JeffDrummond
JeffDrummond

asked on

Insert row in header of ASP.Net datagrid

Hello:

I am developing a report that uses a datagrid with about 15 columns.
The area of the report above the last two column headings contains
some special instructions about those two columns.

Can I add a row to the datagrid header above the row that
has all the column headings?  If that is possible I think I will
be able to do some formatiing of the last two headers programatically.

Thanks!
Avatar of Volkan Vardar
Volkan Vardar

i advice you to use repeater instead of the datagrid.
by this way you can put as many rows as you wish in the <headertemplate> of the repeater.
Hi,

In addition of vardium you can access the header control from the codebehind like here :
http://www.howtodothings.com/showarticle.asp?article=695

Regards,
B..M
Avatar of JeffDrummond

ASKER

Thanks Vardium, but development is too far along to switch controls now.

Regarding the article, I don't understand how to add a row to the datagrid
header using the example given.  I see how you can get to the controls
in the header, but adding a row?

I'm trying this in the datagrid ItemDataBound event:

            If e.Item.ItemType = ListItemType.Header Then
                Dim header As Control = CType(sender, DataGrid).Controls(0).Controls(0)

==>>  What next?

            End If

Thanks!  I appreciate your help.
SOLUTION
Avatar of mmarinov
mmarinov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Say the datagrid header contains the following columns:

________________________________
     Author    |    Title    |    Date         |
---------------------------------------------

I want to add a row to the header above the column titles:

________________________________
___________|________|___________|
     Author    |    Title    |    Date         |
---------------------------------------------

I've been working with your example above but I do not
know enough to get it to work.

Thanks.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I got this working with a slight addition to ipmcc's code as follows:

            private void grdSchedule_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
            {
                  if (e.Item.ItemType == ListItemType.Header)
                  {
                        // make a new item that you want to insert -- it must be a datagrid item -- its joining
                        // the existin header, so set it with all the same params    
                        DataGridItem myItem = new DataGridItem(e.Item.ItemIndex, e.Item.DataSetIndex, e.Item.ItemType);
                        foreach (TableCell cell in e.Item.Cells)
                        {
                              myItem.Cells.Add(new TableCell());
                        }

                        foreach (TableCell cell in myItem.Cells)
                        {
                              cell.Text = "AAA";
                        }
     
                        // add various, hopefully useful stuff to your new row here...
                        ((DataGrid)sender).Controls[0].Controls.Add(myItem);
                  }            
            }

I want some of my top cells to span mroe than one column though which I'm looking at now.  I'll come back if I figure out how to do it.

Dave
Ok figured this out.

You can set the columnspan property as follows:

                        foreach (TableCell cell in myItem.Cells)
                        {
                              cell.Text = "AAA";
                              cell.ColumnSpan = 2;
                        }

Which means thet the code which originally inserts the cells must change to insert less cells as appropriate.  For example use:

                        for (int i=0; i < 10; i ++)
                        {
                              myItem.Cells.Add(new TableCell());
                        }

But change the 10 to match your requirements

Dave