We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

GridDataBoundGrid Column Headers

When I attach to a DataTable, I am expecting the column header to reflect the DataColumn.Caption property. It seems that this should be okay, but the grid seems to be displaying the DataColumn.ColumnName (which, per the docs is what is returned if the Caption property is not explicitly set) rather than the value of the Caption property. Am I missing something or is this designed behavior? I just checked the MS DataGrid and it ignores the property too...just _seemed_ like this is what that property of the DataColumn should be used for... I can get the value to work okay with the GridBoundColumn.HeaderText property, but wanted to keep this type of business logic (IMHO) seperated from the UI if possible and only have to have it at the source of the information. Thanks in advance for any responses... Jeff Block

2 Replies

AD Administrator Syncfusion Team August 9, 2003 08:52 PM UTC

The databinding supports any IList or IListSource object. The only information for such general data source is the PropertyDescriptor. So, the HeaderText is initialized with the PropertyDescriptor.Name property. This is why things work as they do in a GridDataBoundGrid and in a DataGrid. That said, it would be possible to add checks for particular datasources to make use of additional properties that might be available. But just changing this functionality at this point would break the behavior of existing code. So, any changes would have to be implemented on a conditional basis. As a workaround, you could add the method below to your form and just call it after you set the datasource to the grid. This would allow you to control the headers just by setting the column captions.
public void SetCaptionIntoHeaderText(DataTable dt, GridDataBoundGrid grid)
{
	GridBoundColumnsCollection gbcc = grid.GridBoundColumns;
	if(gbcc.Count == 0)
		gbcc = grid.Binder.InternalColumns;
	if(gbcc.Count > 0)
	{
		foreach(GridBoundColumn gbc in gbcc)
		{
			DataColumn dc = dt.Columns[gbc.MappingName];
			if(dc.Caption.Length > 0 && !dc.Caption.Equals(gbc.HeaderText))
			{
				gbc.HeaderText = dc.Caption;
			}
		}
	grid.RefreshRange(GridRangeInfo.Row(0));
	}
}


JB Jeffrey Block August 10, 2003 08:15 PM UTC

Thanks Clay, I figured as much, but it never hurts to ask! ;>) Thanks for the snippet too. I modified a bit to set a flag to see if the refresh was required, e.g. were any column headers actually changed.

Loader.
Live Chat Icon For mobile
Up arrow icon