By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,533 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

DataGrid - adding an extra header...

P: n/a
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/247referenc.../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
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <sa****@discussions.microsoft.com> wrote in message
news:C0**********************************@microsof t.com...
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/247referenc.../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


Nov 19 '05 #2

P: n/a
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 [Microsoft MVP]" wrote:
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" <sa****@discussions.microsoft.com> wrote in message
news:C0**********************************@microsof t.com...
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/247referenc.../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


Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.