Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WPF > GridView > Bind Datatable with RadGridView

Not answered Bind Datatable with RadGridView

Feed from this thread
  • Moon avatar

    Posted on Feb 4, 2012 (permalink)

    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!

    Reply

  • Moon avatar

    Posted on Feb 27, 2012 (permalink)

    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!

    Reply

  • Didie Didie admin's avatar

    Posted on Feb 28, 2012 (permalink)

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

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WPF > GridView > Bind Datatable with RadGridView
Related resources for "Bind Datatable with RadGridView"

WPF Grid Features  |  Documentation  |  Demos  |  Telerik TV  |  Self-Paced Trainer  ]