This is a migrated thread and some comments may be shown as answers.

Autogenerated Columns from DataTable.Columns should use Caption for HeaderText

5 Answers 299 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 08 Aug 2011, 05:02 PM
Hi,

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

Sort by
0
Vlad
Telerik team
answered on 09 Aug 2011, 06:48 AM
Hello Thomas,

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

0
Thomas
Top achievements
Rank 1
answered on 09 Aug 2011, 03:44 PM
Hi Vlad,

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" />
0
Vlad
Telerik team
answered on 09 Aug 2011, 03:49 PM
Hello Thomas,

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

0
Swapnil
Top achievements
Rank 1
answered on 11 Sep 2015, 07:23 AM

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!!!

 

 

 ​

 

 

 ​

0
Stefan
Telerik team
answered on 15 Sep 2015, 03:23 PM
Hello Swapnil,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Thomas
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Thomas
Top achievements
Rank 1
Swapnil
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or