Solved

Entity Framework object edit/add dialog window

Posted on 2010-09-10
9
310 Views
Last Modified: 2012-05-10
I am using the Entity/Framework and with SQL & LINQ to create a new application.  I have run into a dilemma and after much searching around on the internet I haven’t been able to find the “best practice” that I am looking for.  Here is the situation.

Most of the EF examples keep it pretty simple.  
1)      Put a grid on your form
2)      Bind the grid to a basic linq query
3)      Allow the user to edit the data right in the grid

I am finding that I get into more complex areas of the application I need to add another step to this.  Instead of letting the user edit the data right in the grid, I want to have then double click it (or select it in some other method) and open a dialog window to edit the values for the object.   That would allow for more validation and  a better UI for end users.  

I have done this a couple different ways, usually by passing the EF Context object and the object to edit to the dialog.  I feel as though I am making it harder than it should be.  Does anyone have any good examples of the “best practice” for editing & adding EF objects via dialog windows rather than in the grids themselves?

Thanks in advance..
0
Comment
Question by:MRS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33652842
The following sample is done in WPF..

Window2.xaml
public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();
            this.DataContext = this;
            Ent = new FinancialsEntities();
        }


        public FinancialsEntities Ent { get; set; }
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            Window3 wind = new Window3();
            wind.DataContext = this.SelectedItem;
            wind.ShowDialog();
            if (wind.Cancelled)
            {
                Ent.Refresh(System.Data.Objects.RefreshMode.StoreWins, this.SelectedItem);
            }
        }

        public object SelectedItem { get; set; }

        public IQueryable Items
        {
            get
            {
                return from tetmp in Ent.Accounts select tetmp;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Ent.SaveChanges(true);
            this.Close();
        }
    }

Open in new window

0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33652845
Window2.xaml
<Window x:Class="Tes1234.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="300" Width="300" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="24"/>
        </Grid.RowDefinitions>
        <ListView Name="listView1" SelectedValue="{Binding SelectedItem}" ItemsSource="{Binding Items}">
            <ListView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Edit" Click="MenuItem_Click"/>
                </ContextMenu>
            </ListView.ContextMenu>
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding AccountNo}"/>
                    <GridViewColumn DisplayMemberBinding="{Binding AccountName}"/>
                </GridView>
            </ListView.View>
        </ListView>

        <Button Width="120" Height="24" HorizontalAlignment="Right" Click="Button_Click" Content="Save" Grid.Row="1"/>
    </Grid>
</Window>

Open in new window

0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33652846
Window3.xaml
<Window x:Class="Tes1234.Window3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window3" Width="350" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="26"/>
            <RowDefinition Height="26"/>
            <RowDefinition Height="26"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="125"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <TextBlock Text="Account No:"/>
        <TextBox Text="{Binding Path=AccountNo}" Grid.Column="1"/>
        
        <TextBlock Text="Account Name:" Grid.Row="1"/>
        <TextBox Text="{Binding Path=AccountName}" Grid.Column="1" Grid.Row="1"/>
        
        <Button Click="Button_Click" Width="75" Height="24" HorizontalAlignment="Right" Content="Ok" Grid.Row="2"/>
        <Button Click="Button_Click_1" Width="75" Height="24" HorizontalAlignment="Right" Content="Cancel" Grid.Row="2" Grid.Column="1"/>
    </Grid>
</Window>

Open in new window

0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33652849
Window3.xaml
public partial class Window3 : Window
    {
        public Window3()
        {
            InitializeComponent();
            this.Cancelled = true;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Cancelled = false;
            this.Close();
        }

        public bool Cancelled { get; set; }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }

Open in new window

0
 

Author Comment

by:MRS
ID: 33661805
I am not super familiar with WPF, what does the datacontext object encapsulate?  Is that a reference to the Context Object?
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33663219
It is not the Context object. It just holds the selected item which in this case is Accounts. We are not passing the EntityContext object to the second window but just the Selected Account item.
0
 

Author Comment

by:MRS
ID: 33663344
So in that can how would you go about using that dialog window for creating a new "Account" item.  Would i pass the a new instantiation of an "Account" item to the window and only add it to context object upon a successfull "OK" click?

If the above is the case it sounds like the "best practice" here is to make the dialog windows as dumb as possible and keep all of my hard logic in my parent form.  The dialog window is really only meant to allow the user to change values.  Committing to the database and other logic like that would remain on the parent window.

Does that sound right?
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 33663732
Yes, thats how it is normally done. As you said, for adding a new account, we will create a dummy account and pass it to the second form and upon user confirmation (clicking ok), it gets added to the context. Also, as you have observed, this will make the child form relatively "dumb".
0
 

Author Closing Comment

by:MRS
ID: 33663990
Thank you for taking the time..  
0

Featured Post

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question