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