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

Entity Framework and Data Bound RadGrid on Child relationship

2 Answers 120 Views
GridView
This is a migrated thread and some comments may be shown as answers.
M
Top achievements
Rank 1
M asked on 06 Sep 2009, 01:45 AM
 Using Visual Studio 2008 SP1 and .NET 3.5 SP1. I have a simple test project with 2 tables, PARENT and CHILD, linked by foreign key. I created the ADO.NET Entity Data Model, and named the relationship that represents the database foreign key (FK_PARENT) to Children in the PARENT entity. My goal here is to bind a grid control to PARENT.Children so the user is editing only child records for the selected parent, and in this case, it is a new PARENT record.

I have a WPF form, with 2 tabs, tab1 and tab2.

tab1 has some fields where I capture data for PARENT, then on tab2 I have a Telerik RadGridView control, bound to parent.Child (which is the association that represents the foreign key from CHILD -> PARENT).

1) I enter the data, then click tab2 (which causes tab1's LostFocus event to fire and I call entities.AddToParent(parent)
2) I use the insert key (for the Telerik RadGridView) which calls the AddingNewDataItem event, which indeed ends up adding the records to my CHILD table just fine.
3) The inserted record has the correct PARENT key.

PROBLEM: The grid shows a new empty row, with no columns, period. Just an empty grid. The database record is actually correct, however.

If I bind the grid directly to entities.CHILD, I see all CHILD columns correctly. It is the binding to the relationship that causes this problem.

To alleviate the data provider, I tested both on SQL Server and SQL Anywhere 11, but get the same results.

Essentially, I have a parent entity in scope, I want to add child entities, and I want a bound control to show those immediately, and also to allow editing.

Code here:

public partial class Window1 : Window
    {
        Entities entities;
        PARENT parent;
        bool saved = false;
 
        public Window1()
        {
            InitializeComponent();
            entities = new Entities();
            parent = PARENT.CreatePARENT(Guid.NewGuid().ToString(), "00000");
            gridViewChild.ItemsSource = parent.Child;
 
        }
 
        private void buttonOk_Click(object sender, RoutedEventArgs e)
        {
            entities.SaveChanges();
        }
 
        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
 
        }
 
        private void tabItemParent_LostFocus(object sender, RoutedEventArgs e)
        {
            if (!saved)
            {
                // Create the parent
                parent.FIRST_NAME = this.textBoxFirstName.Text;
                parent.LAST_NAME = this.textBoxLastName.Text;
                parent.MIDDLE_NAME = this.textBoxMiddleName.Text;
                parent.ZIP = this.textBoxZipCode.Text;
                entities.AddToPARENT(parent);
                saved = true;
            }
        }
 
        private void gridViewChild_AddingNewDataItem(object sender, Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
        {
            // Entity Framework supports identity (server side generated) keys but we
            // must provide a default key value first.
            CHILD child = CHILD.CreateCHILD(0, "Test");
            parent.ChildProfile.Add(child);
            e.NewObject = child;
        }
    }

Help.

2 Answers, 1 is accepted

Sort by
0
M
Top achievements
Rank 1
answered on 06 Sep 2009, 01:59 AM
I tested by dropping in another vendor's grid control (C1) in the same form with the same code and the C1 grid shows the record as soon as it is added, and displays properly.

So this is certainly a bug in the Telerik grid control.
0
Rossen Hristov
Telerik team
answered on 07 Sep 2009, 08:49 AM
Hello M,

Can you please send us your runnable sample project. This will help us investigate this issue much faster than trying to replicate it. Thanks in advance.

Best wishes,
Ross
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
M
Top achievements
Rank 1
Answers by
M
Top achievements
Rank 1
Rossen Hristov
Telerik team
Share this question
or