1

I am currently using Telerik's RadGridView to display data from a database. The data that I want does load into the gridview and I also added three extra columns which are for users to type in additional information.

The problem I'm having is that when you type in information into one of the empty cells and click out of the row/column, the information that I've typed in disappears. I've scoured every forum relating to this and I think that I have the code right using gridView.Items.CommitEdit, but the information that I've inputted into the empty cells still disappears. Here is the code which creates the extra columns:

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        //Loads queries from each of the designated data tables in BSI_Test
        var customerQuery =
            (from customer in testEntity.Customers
             join job in testEntity.Jobs
             on customer.CID equals job.CID
             join claim in testEntity.Claims
             on job.JID equals claim.JID
             select new
             {
                 Customer_Name = customer.CName,
                 Customer_Id = customer.CID,
                 Job_Id = job.JID,
                 Claim_Id = claim.CLAIMID,
                 DID = DeductId,
                 Check_No = CheckNo,
                 Check_Date = CheckDate
             })
            .OrderBy(c => c.Customer_Name);


        //Populates the Telerik data grid with data.
        gridView.ItemsSource = customerQuery.ToList();


        GridViewDataColumn deductId = new GridViewDataColumn();
        deductId.UniqueName = "DeductId";
        deductId.Header = "DID";
        deductId.DataMemberBinding = new Binding("DeductId");
        gridView.Columns.Add(deductId);

        GridViewDataColumn checkNo = new GridViewDataColumn();
        checkNo.UniqueName = "CheckNo";
        checkNo.Header = "Check No";
        checkNo.DataMemberBinding = new Binding("CheckNo");
        gridView.Columns.Add(checkNo);

        GridViewDataColumn checkDate = new GridViewDataColumn();
        checkDate.UniqueName = "CheckDate";
        checkDate.Header = "Check Date";
        checkDate.DataMemberBinding = new Binding("CheckDate");
        gridView.Columns.Add(checkDate);
    }

And here is my gridView_CellEditEnded event that attempts to commit any edits that are made to the columns. Note: I did test this event out using breakpoints and it does make it all the way through the if statement when I type information into the cell and click out of it. However, the data that I have inputted still disappears, so CommitEdit doesn't seem to work properly.

bool handle = true;
private void gridView_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
{
    if (e.EditAction == GridViewEditAction.Commit && handle)
    {
        handle = false;
        gridView.Items.EditItem(this.gridView.CurrentItem);
        gridView.Items.CommitEdit();
        handle = true;
    }
}

If anyone is able to help me out with this, it would be greatly appreciated. I'm honestly very confused as to what is wrong with my code.

EDIT: I have updated the code for the Window_Loaded event to show the linq query that queries the information from the database to the datagrid. The ItemsSource is then set to this query as a list. Below is everything before my MainWindow() method where I set the properties for DeductId, CheckNo, and CheckDate.

public string DeductId { get; set; }
public int CheckNo { get; set; }
public string CheckDate { get; set; }

public MainWindow()
{
    InitializeComponent();
}

EDIT: Now I've added in the DeductId, CheckNo, and CheckDate properties to the linq query.

EDIT: Here is the DataProperties class:

public partial class DataProperties
{
    public string CName { get; set; }
    public int CID { get; set; }
    public int JID { get; set; }
    public int CLAIMID { get; set; }
    public string DeductId { get; set; }
    public string CheckNo { get; set; }
    public string CheckDate { get; set; }
}
GamerTalks
  • 63
  • 9
  • The code you've shown so far works fine, given that the models you're trying to bind actually have properties called "DeductId", "CheckNo" and "CheckDate". Could you show the part of the code which binds your datasource to the grid? In other words, how do you bind or assign the `ItemsSource` of the datagrid? Do you happen to be binding a query directly to the datagrid? – Vincent Aug 14 '17 at 21:08
  • Yes, I am binding a linq query to the `ItemsSource` directly to the grid. The query joins three different tables from the database that I am using and displays the required information. I will edit this post tomorrow once I have access to the application and post the code binding the `ItemsSource`. But for the other properties such as "DeductID", "CheckNo", and "CheckDate", I have those properties programmed separately since they are not part of the database and will post those as well. – GamerTalks Aug 14 '17 at 22:04
  • Alright, I have edited my post to show the linq query that is the ItemsSource and also posted my code where I set the properties for DeductId, CheckNo, and CheckDate. – GamerTalks Aug 15 '17 at 13:11

1 Answers1

1

You should set the DataMemberBinding property of each column to a Binding object:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    GridViewDataColumn deductId = new GridViewDataColumn();
    deductId.UniqueName = "DeductId";
    deductId.Header = "DID";
    deductId.DataMemberBinding = new System.Windows.Data.Binding("DeductId");
    gridView.Columns.Add(deductId);

    GridViewDataColumn checkNo = new GridViewDataColumn();
    checkNo.UniqueName = "CheckNo";
    checkNo.Header = "Check No";
    deductId.DataMemberBinding = new System.Windows.Data.Binding("CheckNo");
    gridView.Columns.Add(checkNo);

    GridViewDataColumn checkDate = new GridViewDataColumn();
    checkDate.UniqueName = "CheckDate";
    checkDate.Header = "Check Date";
    deductId.DataMemberBinding = new System.Windows.Data.Binding("CheckDate");
    gridView.Columns.Add(checkDate);
}

You also need to make sure that the type T of the IEnumerable<T> that you have set as the ItemsSource for the RadGridView actually contains the DeductId, CheckNo and CheckDate properties and that each of these have a public setter.

Binding to collection of objects of an anonymous type won't work. You need to create a class that contains all properties (Customer_Name, Customer_Id, Job_Id, Claim_Id, DeductId, CheckNo and CheckDate) and have public setters for those that you want to be able to edit in the RadGridView:

var customerQuery =
           (from customer in testEntity.Customers
            join job in testEntity.Jobs
            on customer.CID equals job.CID
            join claim in testEntity.Claims
            on job.JID equals claim.JID
            select new YourClass //<--
            {
                Customer_Name = customer.CName,
                Customer_Id = customer.CID,
                Job_Id = job.JID,
                Claim_Id = claim.CLAIMID,
                DID = DeductId,
                Check_No = CheckNo,
                Check_Date = CheckDate
            })
           .OrderBy(c => c.Customer_Name);
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thank you for the response. I have updated my post to show exactly what I am setting as my `ItemsSource`. I am using a linq query because most of my information that I am displaying is from a database. Do I also have to include the three custom made columns into my linq query as well? – GamerTalks Aug 15 '17 at 13:15
  • You should create a new class that includes all the properties, i.e. Customer_Name, Customer_Id, Job_Id, Claim_Id, DeductId, CheckNo and CheckDate, and then do `select new YourClass` in Window_Loaded. – mm8 Aug 15 '17 at 13:20
  • I should mention that I am working with three different data tables, so each of them have their own class with properties. I am pulling them from each of the different classes in my linq query, and then I have separate properties for DeductId, CheckNo, and CheckDate which is for the user to enter in information. I did just manage to put them into the linq query, however now I can't edit the cells at all. I can't click on and type into the cells that are generated from the linq query. – GamerTalks Aug 15 '17 at 13:27
  • Please edit your question and include your current implementation of the Window_Loaded method and your new class. – mm8 Aug 15 '17 at 13:31
  • I've edited the question with my new linq query. The properties that I have set for DeductId, CheckNo, and CheckDate are at the bottom of the question, and I do not have a separate class for them because as of right now I shouldn't need one to solve this problem. – GamerTalks Aug 15 '17 at 13:35
  • As already mentioned you need to create a class that holds all properties. Now you are binding to an anonymous type that has no setters. That's why you can't edit the cells. This won't work: `select new`. This will `select new YourClass`. So yes, you must create a class. – mm8 Aug 15 '17 at 13:40
  • Unfortunately that still does not work. The columns are displayed, but they are still set as ReadOnly and I cannot edit them. I do have the ReadOnly property of the RadGridView set to false, so I'm unsure of why this issue is persisting. – GamerTalks Aug 15 '17 at 13:57
  • Post your class. – mm8 Aug 15 '17 at 14:01
  • Above is my class. although the issue is going to be still retrieving the data from the actual database, as those are in a separate entity. – GamerTalks Aug 15 '17 at 14:06
  • Are you doing `select new DataProperties`? – mm8 Aug 15 '17 at 14:08
  • actually, I just realized that I had some of it programmed wrong after seeing your edited answer. I'm going to try something else real quick. – GamerTalks Aug 15 '17 at 14:08
  • Wow, I feel like an idiot. I misunderstood what you were talking about and I missed the fact that you had updated your answer. I originally put DataProperties inside the brackets after `select new` which is what caused the problems. Thank you, you are absolutely a life saver :) – GamerTalks Aug 15 '17 at 14:12