Change the datagrid column header text / title

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";

//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];
Change the datagrid column header text / title Change the datagrid column header text / title Reviewed by czetsuya on Tuesday, September 29, 2009 Rating: 5


vinay said...

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

Add style.MappingName="table name";

This is working!

czetsuya said...

Hi, yes your right forgot to paste that part.

Again this part is very important:

style.MappingName="table name";

Thanks :-D

Powered by Blogger.