Visual Studio Developer Center > Visual Studio Forums > Visual Studio Report Controls > ASPNET GridView how to rename column header?
Ask a questionAsk a question
 

Proposed AnswerASPNET GridView how to rename column header?

  • Wednesday, November 04, 2009 2:13 PMChui Kean Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Dear All,

    I really can't imagine how difficult to rename just a column header.

    I am using LINQ to query something like this.

    var Users = from u in Db.UserTbls select u;

     mGridView.DataSource = Users;

    mGridView.DataBind();

    There is a "UserID" column which I wish to rename to "User ID". And leave the rest (auto generated).

    But unfortunately there isn't a way I can do this easily.

    I manage to rename it in the RowCreated call back. But then it erased other formating (like the capability to sort). :(

     

All Replies

  • Friday, November 06, 2009 5:02 PMJohnFL Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed AnswerHas Code

    Try This

            With Me.GridView1
                .DataSource = ds 'DataSet
                .DataMember = "Region" 'DataTable
                .DataBind()
            End With
    
            With Me.GridView1.HeaderRow
                .Cells(0).Text = "Region ID"
                .Cells(1).Text = "Region"
            End With
    
    

    John
    • Proposed As Answer byJohnFL Friday, November 13, 2009 11:09 AM
    •  
  • Sunday, November 08, 2009 4:39 AMChui Kean Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    JohnFL,

    This removed the html format at the header. Say if I enable sorting for that column, it will be removed after I change the text.
  • Monday, November 09, 2009 1:59 AMJohnFL Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Can't you enable sorting back in Code?


    John
  • Monday, November 09, 2009 11:23 PMChui Kean Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The sorting is enabled all the while, and the proper html/javascirpt required for the sorting is already inserted into the column header properly.

    When I change the header using the method mentioned above, it overided the codes with just a plain text.

    Hence there is no way for me to put it back.

    One work arond is ..... Text.Replace("UserID, "User ID");

    But this looks very ugly.

    So, at the end I have to use LinqDataSource instead, and add all the columns manually. And then I am facing all sort of other issues which is very difficult to work around :(

    http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/671d64bf-2e8e-42ee-a2e3-12b8cdb5802b

    Linq in GridView is not as flexible as it claimed, and still full with un-wanted *features* (a.k.a BUGS)
  • Friday, November 13, 2009 11:14 AMJohnFL Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Chui,
    You need to post a new question about sorting GridView after renaming bound columns. This post has been answered based on the question on how to rename column header in gridview.

    This way you will have a new audience with new question :--)

    Thanks
    John