Click here to Skip to main content
15,917,610 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i just want to display a table from database to a datagrid which i can do.but i need to customize the datagrid headers.like if a table consist of 4 fields like name1,name2,date1 and date2.i need to add two headers above this fields like name and date.name1 and name2 should come under name and date1 and date2 should come under date.like this way i need to customize the datagrid.i am not having any idea how to do it.can anyone pls help me? .
Posted

1 solution

You can use something like this:
C#
protected void Grid1_ItemCreated(object sender, DataGridItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Header)
    {
        DataGrid grid = sender as DataGrid;
        DataGridItem dgi = new DataGridItem(0, e.Item.DataSetIndex, ListItemType.Header);
        dgi.Cells.Add(new TableCell { ColumnSpan = 2, Text = "Name" });
        dgi.Cells.Add(new TableCell { ColumnSpan = 2, Text = "Date" });
        Table t = grid.Controls[0] as Table;
        t.Rows.AddAt(0, dgi);
    }
}

I don't know exact layout of your grid so you might have to add more cells to match your design.
It would be a bit more complicated if you use auto-generated columns, but the principle is the same.
 
Share this answer
 
Comments
amaljosep 29-Mar-12 1:00am    
thanks a lot for ur helps.it works for me.......i am having one query like whether we can allign the values in the column to the center in the newly created rows.

for eg whether we can allign name and date to the center of the column in the above example???
sjelen 29-Mar-12 7:37am    
You want "Name" centered across "Name1" and "Name2"?
Try:
dgi.Cells.Add(new TableCell { ColumnSpan = 2, Text = "Name", HorizontalAlign= HorizontalAlign.Center });
Or if you're using CSS class to style table:
dgi.Cells.Add(new TableCell { ColumnSpan = 2, Text = "Name", CssClass="..." });
amaljosep 2-Apr-12 10:01am    
it worked...once again thanks a lot for helping me.......

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900