while evaluation RadGridView I'm wondering why in case of AutoGenerated columns bound to an DataTable
the HeaderText is taken from ColumnName instead of Caption.
All I found about this 'issue' is that old post:
http://www.telerik.com/community/forums/aspnet/grid/should-radgrid-bind-caption-field-of-datatable-colums-to-gridboundcolumn-headertext.aspx#404028
Telling that this might be changed in future, has this been changed or is there a other solution in MVVM environment (without doing it in code behind)?
Any help is welcome.
Regards
Thomas
5 Answers, 1 is accepted
You can create the grid columns in your view model similar to the application in this thread. The project is for Silverlight however the approach in WPF is exactly the same.
Regards,Vlad
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
thanks for the answer.
Based on your suggestion I wrote an Markup extension that solves this header 'problem' and my the second problem of resizing the columns. To help somebody who has the same problem I attached my solution:
Thanks again
Thomas
public
class
DataColumnMarkupExtensions {
private
RadGridView Grid {
get
;
set
; }
#region Extension
private
static
DataColumnMarkupExtensions GetDataColumnMarkupExtensions(DependencyObject obj) {
return
(DataColumnMarkupExtensions) obj.GetValue(DataColumnMarkupExtensionsProperty);
}
private
static
void
SetDataColumnMarkupExtensions(DependencyObject obj, DataColumnMarkupExtensions value) {
obj.SetValue(DataColumnMarkupExtensionsProperty, value);
}
private
static
readonly
DependencyProperty DataColumnMarkupExtensionsProperty =
DependencyProperty.RegisterAttached(
"DataColumnMarkupExtensions"
,
typeof
(DataColumnMarkupExtensions),
typeof
(DataColumnMarkupExtensions),
null
);
#endregion
#region ColumnItems
public
static
DataColumnCollection GetColumnItems(DependencyObject obj) {
return
(DataColumnCollection) obj.GetValue(ColumnItemsProperty);
}
public
static
void
SetColumnItems(DependencyObject obj, DataColumnCollection value) {
obj.SetValue(ColumnItemsProperty, value);
}
public
static
readonly
DependencyProperty ColumnItemsProperty =
DependencyProperty.RegisterAttached(
"ColumnItems"
,
typeof
(DataColumnCollection),
typeof
(DataColumnMarkupExtensions),
new
PropertyMetadata(
null
, OnColumnItemsChanged));
private
static
void
OnColumnItemsChanged(DependencyObject grid, DependencyPropertyChangedEventArgs e) {
var ActGrid = grid
as
RadGridView;
var oldValue = e.OldValue
as
DataColumnCollection;
var newValue = e.NewValue
as
DataColumnCollection;
if
(ActGrid !=
null
) {
var Extension = GetDataColumnMarkupExtensions(ActGrid);
if
(Extension ==
null
) {
Extension =
new
DataColumnMarkupExtensions { Grid = ActGrid };
SetDataColumnMarkupExtensions(ActGrid, Extension);
}
ActGrid.Columns.Clear();
if
(oldValue !=
null
) {
oldValue.CollectionChanged -= Extension.OnColumnItemsCollectionChanged;
}
if
(newValue !=
null
) {
foreach
(DataColumn Item
in
newValue) {
AddColumn(Item, ActGrid);
}
newValue.CollectionChanged += Extension.OnColumnItemsCollectionChanged;
}
}
}
private
void
OnColumnItemsCollectionChanged(
object
sender, System.ComponentModel.CollectionChangeEventArgs e) {
if
(e.Action == CollectionChangeAction.Refresh) {
this
.Grid.Columns.Clear();
}
else
if
(e.Action == CollectionChangeAction.Remove){
var Col =
this
.Grid.Columns[((DataColumn)e.Element).ColumnName];
this
.Grid.Columns.Remove(Col);
}
else
if
(e.Action == CollectionChangeAction.Add){
AddColumn((DataColumn) e.Element,
this
.Grid);
}
}
private
static
void
AddColumn(DataColumn dataCol, RadGridView grid) {
GridViewBoundColumnBase GridColumn =
new
GridViewBoundColumnBase();
GridColumn.Header = dataCol.Caption;
GridColumn.Width = grid.ColumnWidth;
GridColumn.Name = dataCol.ColumnName;
GridColumn.UniqueName = dataCol.ColumnName;
GridColumn.DataType = dataCol.DataType;
GridColumn.DataMemberBinding =
new
System.Windows.Data.Binding(dataCol.ColumnName);
grid.Columns.Add(GridColumn);
}
#endregion
#region ColumnSize
public
static
GridViewLength GetColumnSize(DependencyObject obj) {
return
(GridViewLength) obj.GetValue(ColumnSizeProperty);
}
public
static
void
SetColumnSize(DependencyObject obj, GridViewLength value) {
obj.SetValue(ColumnSizeProperty, value);
}
public
static
readonly
DependencyProperty ColumnSizeProperty =
DependencyProperty.RegisterAttached(
"ColumnSize"
,
typeof
(GridViewLength),
typeof
(DataColumnMarkupExtensions),
new
PropertyMetadata(GridViewLength.Auto, OnColumnSizeChanged));
private
static
void
OnColumnSizeChanged(DependencyObject grid, DependencyPropertyChangedEventArgs e) {
var ActGrid = grid
as
RadGridView;
var newValue = (GridViewLength) e.NewValue;
if
(ActGrid !=
null
) {
var Extension = GetDataColumnMarkupExtensions(ActGrid);
if
(Extension ==
null
) {
Extension =
new
DataColumnMarkupExtensions { Grid = ActGrid };
SetDataColumnMarkupExtensions(ActGrid, Extension);
}
foreach
(var Col
in
ActGrid.Columns) {
Col.Width = newValue;
}
}
}
#endregion
}
<
telerik:RadGridView
Name
=
"GridViewMain"
IsReadOnly
=
"True"
inh:DataColumnMarkupExtensions.ColumnItems
=
"{Binding DefaultViewColumns}"
inh:DataColumnMarkupExtensions.ColumnSize
=
"{Binding GridColumnsSizeMode}"
ItemsSource
=
"{Binding Path=DefaultView}"
AutoGenerateColumns
=
"False"
/>
Thanks for sharing! I've added 5000 Telerik points to your account.
All the best,Vlad
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
I am binding a DataTable to RadGridView and AutoGenerateColumns="True",
Following are the columns I have,
dt.Columns.Add("Text", typeof(string));
dt.Columns.Add("Number", typeof(double));
dt.Columns.Add("CustomType", typeof(Custom));
Custom is a Enum as follows,
public enum Custom
{
One,
Two,
Three
}
Now when I bind the DataTable to RadGridView it displays Data correnctly, but when I try to change the value of custom columns I am not getting dropdown to select a value from Enum instead when I enter in edit mode it displays the Id of the selected Enum.
I tried changing RadGridViewDataColumn to RadGridViewComboBoxColumn in AutoGeraratingColumns Event but not able to make it working.
Please help as its pretty urgent and important.
Attached is the screenshot for the same.
Thanks in Advance!!!
Can you please take a look at the Bind GridViewComboBoxColumn to Enum values forum thread, as this topic is already discussed in it?
Additionally, you can take a look at the ComboBox Column online demo.
Let me know in case you need further assistance.
Best Regards,
Stefan
Telerik