DataGrid - adding an extra header...

G

Guest

I was wondering if there is a way I can add an extra header to a datagrid?

I found this solution on the internet - but it seems quite old and didn't
work for me.

http://www.dotnet247.com/247reference/msgs/13/69744.aspx

It recommends the following:

Private Sub DataGrid1_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.PreRender
Dim dgitem As New DataGridItem(0, 0, ListItemType.Header)
Dim mycell As New TableCell
mycell.ColumnSpan = 1 'Set it to the colspan that you want
mycell.Text = "PRESS RADIO TV"
dgitem.Cells.Add(mycell)
DataGrid1.Controls(0).Controls.AddAt(0, dgitem)
End Sub

But I get an error:

Specified argument was out of the range of valid values. Parameter name:
index

I appreicate your help,


KS
 
K

Ken Cox [Microsoft MVP]

Hi,

You didn't say what kind of header you want, but here's a way to distribute
column headings over two lines by adding a second header.

Ken
Microsoft MVP [ASP.NET]

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, _
ByVal e As _
System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Header Then
Dim dgItemHeader As New DataGridItem _
(0, 0, ListItemType.Header)
Dim intCount As Integer
Dim tcells As TableCellCollection
tcells = e.Item.Cells
Dim fcell As TableCell
Dim blnToggle As Boolean
For intCount = 0 To tcells.Count - 1
fcell = New TableCell
blnToggle = Not blnToggle
If blnToggle Then
fcell.Text = tcells(intCount).Text
tcells(intCount).Text = ""
Else
fcell.Text = ""
End If
dgItemHeader.Cells.Add(fcell)
Next
DataGrid1.Controls(0).Controls.Add(dgItemHeader)
End If
End Sub


Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="StringValue" HeaderText="String
Value"></asp:BoundColumn>
<asp:BoundColumn DataField="IntegerValue" HeaderText="Integer
Value"></asp:BoundColumn>
<asp:BoundColumn DataField="Boolean"
HeaderText="Boolean"></asp:BoundColumn>
<asp:BoundColumn DataField="CurrencyValue" HeaderText="Currency
Value"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
 
G

Guest

Thanks for your response Ken,

Just to clear things up.

I wanted to create an extra header above the header already present and I
want there to be 4 cells in that header which which cover a certain rowspan
each.

I have modified your exmaple to create exactly what I want:

Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, _
ByVal e As _
System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Header Then
Dim dgItemHeader As New DataGridItem _
(0, 0, ListItemType.Header)

Dim fcell As TableCell
Dim i As Integer
For i = 0 To 3
fcell = New TableCell
fcell.ColumnSpan = 3

Select Case i
Case 0
fcell.ColumnSpan = 2
fcell.Text = ""
Case 1
fcell.Text = "Press"
Case 2
fcell.Text = "Radio"
Case 3
fcell.Text = "Tv"
End Select
fcell.HorizontalAlign = HorizontalAlign.Center
dgItemHeader.Cells.Add(fcell)
Next i

DataGrid1.Controls(0).Controls.AddAt(0, dgItemHeader)
End If
End Sub


Thanks for your help,

regards,

KS

Ken Cox said:
Hi,

You didn't say what kind of header you want, but here's a way to distribute
column headings over two lines by adding a second header.

Ken
Microsoft MVP [ASP.NET]

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, _
ByVal e As _
System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Header Then
Dim dgItemHeader As New DataGridItem _
(0, 0, ListItemType.Header)
Dim intCount As Integer
Dim tcells As TableCellCollection
tcells = e.Item.Cells
Dim fcell As TableCell
Dim blnToggle As Boolean
For intCount = 0 To tcells.Count - 1
fcell = New TableCell
blnToggle = Not blnToggle
If blnToggle Then
fcell.Text = tcells(intCount).Text
tcells(intCount).Text = ""
Else
fcell.Text = ""
End If
dgItemHeader.Cells.Add(fcell)
Next
DataGrid1.Controls(0).Controls.Add(dgItemHeader)
End If
End Sub


Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="StringValue" HeaderText="String
Value"></asp:BoundColumn>
<asp:BoundColumn DataField="IntegerValue" HeaderText="Integer
Value"></asp:BoundColumn>
<asp:BoundColumn DataField="Boolean"
HeaderText="Boolean"></asp:BoundColumn>
<asp:BoundColumn DataField="CurrencyValue" HeaderText="Currency
Value"></asp:BoundColumn>
</Columns>
</asp:DataGrid>

saleek said:
I was wondering if there is a way I can add an extra header to a datagrid?

I found this solution on the internet - but it seems quite old and didn't
work for me.

http://www.dotnet247.com/247reference/msgs/13/69744.aspx

It recommends the following:

Private Sub DataGrid1_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.PreRender
Dim dgitem As New DataGridItem(0, 0, ListItemType.Header)
Dim mycell As New TableCell
mycell.ColumnSpan = 1 'Set it to the colspan that you want
mycell.Text = "PRESS RADIO TV"
dgitem.Cells.Add(mycell)
DataGrid1.Controls(0).Controls.AddAt(0, dgitem)
End Sub

But I get an error:

Specified argument was out of the range of valid values. Parameter name:
index

I appreicate your help,


KS
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top