Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > How do I change the headertext of a datagrid column programmatically

Reply
Thread Tools

How do I change the headertext of a datagrid column programmatically

 
 
Ravi Gudlavalleti
Guest
Posts: n/a
 
      06-17-2004
Hi,

I have a datagrid to view the results from a database.
I am using autogenerate because the number of columns returned from the
database change.
I would like to change the column headers as the ones returned from the
database have no spaces
(example: need to change "EMP_NAME" to "EMPLOYEE NAME" or just "NAME")

I tried the following command
datagrid.columns(0).headertext = "NAME"
datagrid.columns(1).headertext = "SSN"

but it didnt work. I said index out of range. When I tried to see how many
columns were there in the grid using
command
lable.text = datagrid.columns.count
it returned 0 no matter how many columns I had.

How do I get around this problem? Any suggestions?

Thanks in advance.


 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      06-17-2004
Hi Ravi,

The ItemCreated event gives you a chance to change the headers, but you
still need to know the possible data field names.

Give this a try and let us know if it helps?

Ken
Microsoft MVP [ASP.NET]


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

Private Sub DataGrid1_ItemCreated _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemCreated
Dim intItems As Integer
If e.Item.ItemType = ListItemType.Header _
And e.Item.Cells.Count > 0 Then
For intItems = 0 To e.Item.Cells.Count - 1
Select Case e.Item.Cells(intItems).Text
Case "EMP_NAME"
e.Item.Cells(intItems).Text = "Employee Name"
Case "SSN"
e.Item.Cells(intItems).Text = "Social Insurance No."
End Select
Next
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 _
("EMP_NAME", GetType(String)))
dt.Columns.Add(New DataColumn _
("SSN", GetType(String)))
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)).ToString
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource


"Ravi Gudlavalleti" <(E-Mail Removed)> wrote in message
news:40d1fceb$(E-Mail Removed)...
> Hi,
>
> I have a datagrid to view the results from a database.
> I am using autogenerate because the number of columns returned from the
> database change.
> I would like to change the column headers as the ones returned from the
> database have no spaces
> (example: need to change "EMP_NAME" to "EMPLOYEE NAME" or just "NAME")
>
> I tried the following command
> datagrid.columns(0).headertext = "NAME"
> datagrid.columns(1).headertext = "SSN"
>
> but it didnt work. I said index out of range. When I tried to see how many
> columns were there in the grid using
> command
> lable.text = datagrid.columns.count
> it returned 0 no matter how many columns I had.
>
> How do I get around this problem? Any suggestions?
>
> Thanks in advance.
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Programmatically set headertext of a boundfield column John Mason ASP .Net 0 08-20-2006 06:27 AM
Change HeaderText in datagrid Řyvind Isaksen ASP .Net 1 11-06-2005 02:19 PM
change HeaderText of DataGrid in PreRender(). Andy Wang ASP .Net Datagrid Control 6 05-16-2004 09:49 AM
change HeaderText of DataGrid in PreRender() Andy Wang ASP .Net 0 05-12-2004 08:56 PM



Advertisments