Hi,
I'm using a DataTable as the Datasource for a xamDataGrid. I have set the column captions for the columns directly on the datatable, but the grid is still using the column names as captions when it is displayed on screen. Is there a property / setting I'm missing that will make the grid use the captions instead of the names?
Thanks,Jacquers
Hello Jacquers.
The names of the Fields in the XamDataGrid have to exactly the same as the names of the Columns in the DataTable. If not, the grid will do that automatically. You can set the Label property of the Field to what you want to be displayed as text, but the Name has to be the same.
You should also set AutoGenerateFields to False, to ensure that the XamDataGrid will not generate them automatically.
For example:
<igDP:Field Name="FirstName" Label="Employee First Name" />
and the column's name in my DataTable is "FirstName".
You can also take a look at the example in the XamFeatureBrowser regarding Field Layouts.
Hope this helps,
Alex.
Thanks for your quick reply.
Ok, I understand that the name of the column in the grid has to be the same as the name of the column in the datatable and thats fine. I assumed that by setting the column caption in the datatable that this would automatically set the label property of the field to the same value in the grid?
Unfortunately I can't turn the Autogenerate columns off as I'm using databinding and the number of columns will be dynamic, so I can't harcode the values.
In this case, I suppose it is best to use the FieldLayoutInitialized event of the XamDataGrid and set the corresponding caption < - > label.
Does this work in your scenario?
I'll have to take a look into that option, thank you for the suggestion.
Or maybe have a look at the source code of the grid if I'm feeling brave :)
Please attach a sample project with your application or the code you are using.
I have created a simple application myself with the following code and it works as expected. The caption is picked up as the display text (label) of the field.
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn() { ColumnName = "Column1", Caption = "Caption Col1" });
dt.Columns.Add(new DataColumn() { ColumnName = "Column2", Caption = "Caption Col2" });
dt.Columns.Add(new DataColumn() { ColumnName = "Column3", Caption = "Caption Col3" });
dt.Columns.Add(new DataColumn() { ColumnName = "Column4", Caption = "Caption Col4" });
dt.Rows.Add(1, 2, 3, 4);
xamDataGrid1.DataSource = dt.AsEnumerable();
I notice that if you set it as DefaultView (not as enumerable) the Caption property is not respected.
Well, I'm setting a datacontext in the code-behind and in the gui im using the following xaml:
<igDP:XamDataGrid DataSource="{Binding ContactData}" Margin="0,0,0,27" Name="xamDataGrid1" />
I'm using a MVVM pattern, so I dont have direct access to the datagrid from the viewmodel to use the .asEnumerable option. I assume the type of binding that I'm using is using the defaultview option.
Jacquers,
Technically, we think this is not an issue. The ITypedList that is implemented by the DataView returns PropertyDescriptors for the DataColumns, but they do not override the DisplayName and return the Caption of the column. We are considering finding a way around this so that the Captions are respected and override the Field's Label property.
I am glad I helped. I am going to investigate this for you and follow up here in this thread.
Thanks, your sample code helped a lot!
Here is my code:
DataView dv = xamDataGrid1.DataSource as DataView; for (int i = 0; i < dv.Table.Columns.Count; i++) { xamDataGrid1.FieldLayouts[0].Fields[i].Label = dv.Table.Columns[i].Caption; }
Should I submit a bug report for the xamDatagrid not doing this correctly out of the box?
If you go with the FieldLayoutInitialized event, you can get the DataSource of the XamDataGrid as DataView and again make the label - caption adjustments.
DataView dv = xamDataGrid1.DataSource as DataView;
for (int i = 0; i < dv.Table.Columns.Count; i++)
{}