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

Updating a DataTable with RadGridView

4 Answers 517 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 30 Aug 2010, 10:21 PM

I am trying to update a DataTable using a RadGridView. The data is displayed but whenever a column is edited and the cursor moved off the column the old data reappears. E.g. the underlying DataTable is not updated.

 

<Window x:Class="TestRadGridEdit.MainWindow"
        xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"
        xmlns:gridView="clr-namespace:Telerik.Windows.Controls.GridView;assembly=Telerik.Windows.Controls.GridView"
        xmlns:Controls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
        xmlns:data="clr-namespace:Telerik.Windows.Data;assembly=Telerik.Windows.Data"       
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Controls:RadGridView Name="radGridView_HeadOfHouse" AutoGenerateColumns="False" ItemsSource="{Binding GetData}" DataContext="{Binding ElementName=radGridView_HeadOfHouse, Path=DataContext}">
            <Controls:RadGridView.Columns>
                <Controls:GridViewDataColumn UniqueName="FirstName" Header="First Name" DataMemberBinding="{Binding FirstName, Mode=TwoWay}" />
                <Controls:GridViewDataColumn UniqueName="LastName" Header="Last Name" DataMemberBinding="{Binding LastName, Mode=TwoWay}" />
            </Controls:RadGridView.Columns>
        </Controls:RadGridView>
    </Grid>
</Window>
using System.Data;
using System.Windows;
using System.Windows.Data;
  
using Telerik.Windows.Controls;
  
namespace TestRadGridEdit
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
  
            Binding bind = new Binding();
            bind.Source = GetData();
            radGridView_HeadOfHouse.SetBinding(RadGridView.ItemsSourceProperty, bind);
        }
  
  
        public DataTable GetData()
        {
            DataTable dt = new DataTable("Testing");
            dt.Columns.Add(new DataColumn("FirstName"));
            dt.Columns.Add(new DataColumn("LastName"));
              
            DataRow dr = dt.NewRow();
            dr["FirstName"] = "Tom";
            dr["LastName"] = "Smith";
            dt.Rows.Add(dr);
              
            dr = dt.NewRow();
            dr["FirstName"] = "Fred";
            dr["LastName"] = "Smith";
            dt.Rows.Add(dr);
              
            dr = dt.NewRow();
            dr["FirstName"] = "Jack";
            dr["LastName"] = "Smith";
            dt.Rows.Add(dr);
              
            dr = dt.NewRow();
            dr["FirstName"] = "Joe";
            dr["LastName"] = "Smith";
            dt.Rows.Add(dr);
              
            dt.AcceptChanges();
            return dt;
        }
    }
}
What am I missing?

4 Answers, 1 is accepted

Sort by
0
Nedyalko Nikolov
Telerik team
answered on 01 Sep 2010, 02:12 PM
Hi Kevin,

You may consider using DataTable.DefaultView instead of DataTable. DataView implements INotifyCollectionChanged and DataRowView implements INotifyPropertyChanged which will save you a lot of code. If you insist to do this via DataTable you have to update data table manually within RadGridView.RowEditEnded event handler.
I hope this will help.

Greetings,
Nedyalko Nikolov
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Valentin
Top achievements
Rank 1
Iron
Iron
answered on 03 Aug 2017, 08:32 AM

Hi Nedyalko Nikolov,

 

I'm trying to update my DataTable like you said, but it's not working. My code :

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();
        }

 

When I'm starting update, the cell value is empty, and when I validated the new value, the cell value go back to the old value.

 

Have you got any idea ?

 

Thank you.

0
Stefan
Telerik team
answered on 08 Aug 2017, 08:08 AM
Hello Valentin,

From the provided information I cannot confirm the exact cause for this behavior. Can you please confirm whether the validation logic that you mentioned passes and the revert of the input is not due to it?

Regards,
Stefan X1
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 allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Valentin
Top achievements
Rank 1
Iron
Iron
answered on 08 Aug 2017, 08:15 AM

Hi Stefan,

 

I by-passed the problem using a DataGrid to replace RadGridView. It solved a lot of problem that I has.

The problem it was I can't update data using RadGridView. The DataGrid source was not updated. Now, using DataGrid, there isn't problem.

 

Valentin.

Tags
GridView
Asked by
Kevin
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
Valentin
Top achievements
Rank 1
Iron
Iron
Stefan
Telerik team
Share this question
or