Search code examples
c#wpfgridviewtelerikradgridview

Cannot Edit Cells in RadGridView Telerik WPF


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

Solution

  • 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);