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

Bind Datatable with RadGridView

6 Answers 908 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Moon
Top achievements
Rank 1
Moon asked on 05 Feb 2012, 03:15 AM
Hi,

I am new to WPF, I am using MVVM pattern in WPF and generating a Datatable structure at the runtime, then binding Datatable with RadGridView, which is working as expected.

Problem: I want RadGridView configurable (where user can add new row, delete row and edit the data) and on save button click everything should be saved in the database. I am able to bind existing data from the Datatable to RadGridView, but not able to edit, delete or add new rows. I have set "True" value for CanUserDeleteRows and CanUserInsertRows. I am not sure where I am going wrong.

In one of the Telerik blog, I read that we should bind Dataview to RadGridView (as Dataview inplements INotifyPropertyChanged), but if I do that then my RadGridView appears to be empty everytime (not even show coloumns).

Any help will be highly appreciated.

Thanks in advance!

6 Answers, 1 is accepted

Sort by
0
Moon
Top achievements
Rank 1
answered on 27 Feb 2012, 11:47 PM
Hi,

I have asked this question sometime back. But still struggling to find out the answer.... I am not sure whether I was clear to put my question. Thats why I am putting some code snippet as well, so that i makes more sense.

Problem: I want RadGridView configurable (where user can add new row, delete row and edit the data) and on save button click everything should be saved in the database. I have a situation where I need to create an empty DataTable with different columns (depending on user input). And then enter values under those columns and then on click on save need to save value in the database. I am able to bind DataTable to RadGridView (I can see all the column names and data rows if there is already some data in the DataTable in the grid) but NOT able to add (enter) or delete any row at the run time from RadGridView. I have set "True" value for CanUserDeleteRows and CanUserInsertRows. I am not sure where I am going wrong. I am implement INotifyPropertyChanged in my ViewModel class.

My code looks like as below:

ViewModel--

    DataTable _manualDataTable;
   
public DataTable ManualDataTable
   
{
       
get
       
{
           
return _manualDataTable;
       
}
       
set
       
{
            _manualDataTable
= value;
           
OnPropertyChanged("ManualDataTable");
       
}
   
}


For creating DataTable--

    void LoadManualDataTable()
   
{
       
DataTable dtData = new DataTable();
        dtData
.Columns.Add("TimeStamp", typeof(DateTime));
       
List<DataColumn> columns = new List<DataColumn>();
       
var query = _dataContext.GetSenData().Where(sen => sen.LogID == ((DataLogs)SelectedItemNode).Log.LogID).Select(sen => sen.SeriesID);
       
var queryTS = _dataContext.GetDataSeries().Where(ts => query.Contains(ts.SeriesID));
       
foreach (DataSeries ts in queryTS)
       
{
           
var queryPLoc = _dataContext.GetDataLoc().Where(pLoc => pLoc.ParamID == ts.ParamID).Select(pLoc => pLoc.Name);
            dtData
.Columns.Add(queryPLoc.First(), typeof(string));
       
}

       
ManualDataTable = dtData;
     
}

XAML code--

<telerik:RadGridView Grid.Row="0" AutoGenerateColumns="True" ItemsSource="{Binding ManualDataTable}" IsReadOnly="False" CanUserDeleteRows="True"  Name="radGridViewManualDataTable" ShowInsertRow="True" />

DataTable is created as expected (from LoadManualDataTable method) and if I will add any row through the code then those rows will be binded and will be displayed in RadGridView. But I am not able to create or delete rows through RadGridView.


Thanks!
0
Dimitrina
Telerik team
answered on 28 Feb 2012, 08:16 AM
Hello,

 Please bind the ItemsSource of the RadGridView to the DefaultView of the DataTable.  I believe that this will allow you to insert and delete items from the bound collection.

All the best,
Didie
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Minita
Top achievements
Rank 1
Veteran
answered on 08 Apr 2021, 06:33 AM

Hi Dimitrina,

I have one question related to to binding DataTable to RadGridView.

I have one datatable having 12 columns, and I am displaying only 4 columns to RadGridView. Let's say if I am modifying DataTable column(other than 4 shown on UI) values on timer in my code. I am assuming it will affect RadGridView binding as well, as my ItemSource is binded to DataTable.DefaultView. Is my assumption right?

Also I had to go in this details as I facing memory leak issue because of this. I and my team used different memory profiler tools and understood, radgridview is storing and creating multiple instances of that one column which is updating in code on timer even though it's not binded to any of the GridViewDataColumn.

Is there any way to keep the column in DataTable, but do not include it while we bind ItemSource of grid to DefaultView?

Let me know if you have seen this kind of issue and have fixed it in any way earlier.

Thank You,

Minita

0
Vladimir Stoyanov
Telerik team
answered on 12 Apr 2021, 12:23 PM

Hello Minita,

Thank you for the shared information. 

To ensure that we are on the same page, I have created a sample project based on your description and I am attaching it here for your reference. Can you check it out and let me know, if you can observe the same behavior with it? If that is indeed the case, can you share how you are profiling the application so that I can test this on my end as well? You can send over some pictures of the profiling results and let me know which tool you are using. 

If I am missing something of importance in the sample project, can you let me know what modifications are necessary so that I can observe the same results? This will hopefully allow me to investigate the scenario further and better assist you.

Regards,
Vladimir Stoyanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Minita
Top achievements
Rank 1
Veteran
answered on 20 Apr 2021, 07:35 PM

Hello Vladimir,

Thank you very much for reply. The sample program is correct as per the problem statement. On further analysis from our end, looks like the memory does not increase if there's only one grid is updating. I updated the same sample project and designed TabControl which contains RadGridView. Please find the updated classes at https://github.com/minitachaudhari/Telerik-Projects.git
Steps: 
1. Create more than 2 tabs in InitializeTabView()
2. Build and Run the application
3. Select and switch between all tabs on UI and check memory in Task Manager

Scenario 1: You'll notice memory increases if there are multiple tabs including 100+ rows in each grid.

Scenario 2: Memory is constant if there is only 1 tab including 1000+ rows in grid.

Now we are assuming that garbage collector isn't disposing all the objects which are not in use by tabs which are not visible. It'll be great if you can give us a brief idea about how does it work.

Details: I am using JetBrains dotMemory profiler to investigate memory leak. I have attached result pictures as well.

Thanks & Regards,

Minita

0
Vladimir Stoyanov
Telerik team
answered on 23 Apr 2021, 11:41 AM

Hello Minita,

Thank you for the shared pictures. 

I investigated the updated project and my suggestion is to release the DispatcherTimer instances when the MainWindow is unloaded. 

I am attaching the project updated to demonstrate a possible approach for doing so. Can you check it out and let me know, if it helps?

Regards,
Vladimir Stoyanov
Progress Telerik

Тhe web is about to get a bit better! 

The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.

Tags
GridView
Asked by
Moon
Top achievements
Rank 1
Answers by
Moon
Top achievements
Rank 1
Dimitrina
Telerik team
Minita
Top achievements
Rank 1
Veteran
Vladimir Stoyanov
Telerik team
Share this question
or