DataGrid Column Headers

  • Thread starter Thread starter ElenaR
  • Start date Start date
E

ElenaR

I need to figure out how to name my column headers in a
DataGrid.

In VB6, I could write DataGrid1.Columns(0).Caption
= "ID". What is the format for VB.NET?

Thanks in Advance!
 
Hello Elena,

the WinForms DataGrid control is a little more complex to use in that
respect. In order to specify what column headers you want, you need to
create column styles. Here's an example of how you could do it for a
DataGrid bound to a DataSet:

Dim tableStyle As New DataGridTableStyle
tableStyle.ColumnHeadersVisible = True
tableStyle.MappingName = "Table1" ' This is the name of the
DataSet table the grid is bound to.

For Each column As DataColumn In DataSet1.Tables("Table1").Columns
Dim columnStyle As New DataGridTextBoxColumn
columnStyle.MappingName = column.ColumnName
columnStyle.HeaderText = "<" + column.ColumnName + ">"
tableStyle.GridColumnStyles.Add(columnStyle)
Next

DataGrid1.TableStyles.Add(tableStyle)

This code is first creating a DataGridTableStyle object. This object will
hold onto the set of column styles. Then for each column in the DataSet, it
creates a column style. Note that the mapping name is important because
this is how the grid will retrieve the information from the underlying data
source (the DataSet). In the example above, the header text will be the
same as the column name but with '<' and '>' at each end.

HTH

Antoine
Microsoft Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
Content-Class: urn:content-classes:message
From: "ElenaR" <[email protected]>
Sender: "ElenaR" <[email protected]>
Subject: DataGrid Column Headers
Date: Tue, 21 Oct 2003 06:47:38 -0700
Lines: 8
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcOX2eP8NwPr0XVbTR2GxG7VtQpsAg==
Newsgroups: microsoft.public.dotnet.languages.vb
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:148656
NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

I need to figure out how to name my column headers in a
DataGrid.

In VB6, I could write DataGrid1.Columns(0).Caption
= "ID". What is the format for VB.NET?

Thanks in Advance!


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top