p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning ASP.NET 4 : in C# and VB (http://p2p.wrox.com/forumdisplay.php?f=560)
-   -   changing column header text? (http://p2p.wrox.com/showthread.php?t=88264)

rmanapul August 16th, 2012 09:48 PM

changing column header text?
Hi, i was so grateful to learn new things like LINQ to Entity and i use it a lot and incorporate it on my programming practice.

My problem is how can i change the column header text programmatically? when i use LINQ to Entity, the text in the column header appears as "Item" i tried different ways but all of them only resulted in error. can i change the column header text without using datasource controls, only with LINQ to ENTITY? thanks.

Imar August 17th, 2012 01:51 PM

Hi there,

You can set them up in markup, just as you could do when using a Data Source control.

To do it at run-time, you can access a property like Columns to change the name. You didn't mention which data control you're using, but if you were using a GridView you could do something like this:


GridView1.Columns[0].HeaderText = "Some new header";
Hope this helps,


rmanapul August 21st, 2012 08:13 PM

oh sorry i forgot to mention the name but yes, it's gridview im using. you will have to change it in the markup and not in the code-behind?

Imar August 22nd, 2012 03:21 AM

Hi there,

you will have to change it in the markup and not in the code-behind?
I am not sure I understand this. Didn't my post mention both?


rmanapul August 22nd, 2012 03:33 AM

i tried to do it in the code behind but when i code it the .HeaderText property does not appear and it results to error. do i have to import anything before i use it? im using VB as my language

Imar August 22nd, 2012 03:42 AM

I guess that's near impossible to say without seeing the source code and the exact error message.



rmanapul August 23rd, 2012 08:48 PM

here is the complete code of the program:


Imports AlumniTblModel
Partial Class Management_Sample
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Using myEntities As New AlumniTblEntities

            Dim allProvinces = From province In myEntities.ProvinceTbls
                              Order By province.Province
                              Select province.Province

            ProvinceGrid.DataSource = allProvinces
            ProvinceGrid.Columns[0].HeaderText = "Provinces"

        End Using
    End Sub
End Class

when i typed the HeaderText, the intellisense does not show. and the Text "Provinces" does not appear as red color it's supposed to appear as red colored i think? can you also help me how to do paginating and sorting (when the header title is clicked) in this gridview programmatically? where you can only show 5 or 10 items per page.

Imar August 24th, 2012 02:28 AM

Hi there,

The example I posted was C#. Since you're using VB, you need parenthesis instead of brackets:


ProvinceGrid.Columns(0).HeaderText = "Provinces"
For paging, look at Skip and Take. However, in this case, since you're doing straight forward LINQ queries, I would suggest the EntityDataSource instead as it'll take care off all of this.



rmanapul August 24th, 2012 03:25 AM

When i tried to run the application, visual studio provided me with this message:

"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"

do i need to import something???

Imar August 24th, 2012 03:41 AM

Are you using auto generated columns? If so, they are not added the Columns collection which will remain at zero, causing this error. You can do it like this instead:


ProvinceGrid.HeaderRow.Cells(0).Text = "Provinces"
May I ask what you're trying to accomplish? Is there any reason why you're not explicitly defining the columns in the GridView and instead rely on run-time discovery? Also any reason for not using the EntityDataSource to handle binding, paging and sorting for you?


All times are GMT -4. The time now is 04:50 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.