How to Change the Datagrid Column Header

Using .Net1.1, I experienced a problem on how I could change the header text of a datagrid. Normally, you can set it like this (using the DataGridTableStyle):

DataSet ds = query("Select field_name from table");
datagrid.DataSource = ds.Tables[0];

//and NAME will be displayed as the datagrid's headertext.

DataGridTableStyle style = new DataGridTableStyle();
DataGridColumnStyle colStyle = new DataGridTextBoxColumn();
colStyle.HeaderText = "NAME";
colStyle.MappingName = "field_name";
style.GridColumnStyles.Add(colStyle);
datagrid.TableStyles.Add(style);

//Unfortunately it's not working. 
//An alternative is to rename the column in the sql query:


DataSet ds = query("Select field_name AS NEW_FIELD_NAME from table");
datagrid.DataSource = ds.Tables[0];

2 Comments

  1. Hi your code works fine except for one change in the code.

    Add style.MappingName="table name";

    This is working!

    ReplyDelete
  2. Hi, yes your right forgot to paste that part.

    Again this part is very important:

    style.MappingName="table name";

    Thanks :-D

    ReplyDelete

Post a Comment

Post a Comment

Previous Post Next Post
NERV Open Source
Building production Spring Boot systems?

Explore NERV — open-source Java libraries for audit trails, persistence, exception handling, and reliable event-driven architecture.