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

RadGridView ItemsSource using DataTable

1 Answer 492 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Valentin
Top achievements
Rank 1
Iron
Iron
Valentin asked on 02 Aug 2017, 01:21 PM

Hello Telerik,

 

I'm using a DataTable to set my RadGridView ItemsSource. My grid was set like this :

_Xaml

<telerik:RadGridView x:Name="gridSaisie" Grid.Row="1" HorizontalAlignment="Stretch" Margin="5" ShowGroupPanel="False" CanUserInsertRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" RowIndicatorVisibility="Collapsed" AutoGenerateColumns="False" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" CellValidating="gridSaisie_CellValidating" CellEditEnded="gridSaisie_CellEditEnded" RowEditEnded="gridSaisie_RowEditEnded"/>

_C#

private void ConstructGrid()
        {
            //Création de la colonne de dates
            GridViewDataColumn colonneDate = new GridViewDataColumn();
            colonneDate.DataMemberBinding = new Binding("Date");
            colonneDate.Header = "Jour";
            colonneDate.UniqueName = "Jour";
            colonneDate.DataFormatString = "{0: dd}";
            colonneDate.IsReadOnly = true;
            colonneDate.Width = 150;
            colonneDate.IsSortable = false;
            colonneDate.IsFilterable = false;
            colonneDate.HeaderTextAlignment = TextAlignment.Center;
            //colonneDate.DataType = typeof(DateTime);
 
            this.gridSaisie.Columns.Add(colonneDate);
 
            //Création dynamique des colonnes
            foreach (Variable v in this.ViewModel.Variables)
            {
                GridViewDataColumn colonneVariable = new GridViewDataColumn();
                var bindVariable = new Binding(string.Format("Variable_{0}", v.Id));
                bindVariable.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                bindVariable.StringFormat = "{0:F1}";
                bindVariable.Mode = BindingMode.TwoWay;
                colonneVariable.DataMemberBinding = bindVariable;                
                colonneVariable.Header = v.Code;
                colonneVariable.UniqueName = string.Format("Variable_{0}", v.Id);
                colonneVariable.IsReadOnly = false;
                //colonneVariable.Width = ;
                colonneVariable.IsSortable = false;
                colonneVariable.IsFilterable = false;
                colonneVariable.HeaderTextAlignment = TextAlignment.Center;
                //colonneVariable.DataType = typeof(double);
 
                this.gridSaisie.Columns.Add(colonneVariable);
            }
        }
 
        private void LoadData(int idStation)
        {
            this.gridSaisie.ItemsSource = this.ViewModel.Jours; //Its the DataTable
        }

 

I have somes problems :

1) When I set DataType = typeof(...), the displayed values are empty.

2) StringFormat for the Date doesn't work.

3) When I start updating, the cell value is hidden (I think is linq to '4)')

3) I can't update values in the Grid. I found this topic : http://www.telerik.com/forums/updating-a-datatable-with-radgridview where is it explain that the update will be to do manually. So I didi it :

private void gridSaisie_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
        {
            int numCol = e.Cell.Column.DisplayIndex;
            int numRow = this.gridSaisie.Items.IndexOf(this.gridSaisie.SelectedItem);
            var row = this.ViewModel.Jours.Rows[numRow];
 
            row.BeginEdit();
 
            var colonnes = row.ItemArray;
            colonnes[numCol] = e.NewData.ToString();// Convert.ToDouble(e.NewData.ToString());
 
            row.EndEdit();
            row.AcceptChanges();
            //this.gridSaisie.Rebind();
        }

But there isn't changement.

 

Can you help me ?

 

Thank you.

1 Answer, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 07 Aug 2017, 11:09 AM
Hi Valentin,

Thank you for the provided project.

I have tested the cases mentioned in your first and second question and it is working as expected on my side. Using your code snippets I have created sample project which demonstrates that the DataFormatString and DataType properties are working as expected. As for the third and four cases you can consider using the DefaultView of the DataTable as was mentioned in the provided forum thread. You can read more about this in the Managing Collections section in the Managing Data/Overview help article. In the attached project you can see that when you set the ItemsSource of the RadGridView to the DefaultView of a DataTable the value is shown in edit mode and it is updated correctly.

Regards,
Dinko
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
Valentin
Top achievements
Rank 1
Iron
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or