• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

Entity Framework object edit/add dialog window

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
MRS
Asked:
MRS
  • 6
  • 3
1 Solution
 
Gautham JanardhanCommented:
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
 
Gautham JanardhanCommented:
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
 
Gautham JanardhanCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Gautham JanardhanCommented:
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
 
MRSAuthor Commented:
I am not super familiar with WPF, what does the datacontext object encapsulate?  Is that a reference to the Context Object?
0
 
Gautham JanardhanCommented:
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
 
MRSAuthor Commented:
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
 
Gautham JanardhanCommented:
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
 
MRSAuthor Commented:
Thank you for taking the time..  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now