none
Dynamic search in entity framework RRS feed

  • Question

  • Hi,

    I currently try to extend NavigationToolBar with a search functionality. The idea is using the BindingSource to access the Entity Framework ObjectQuery then using a given parameter to specify the table field where to search a value given in my toolbar TextBox. The point is to get it generic on any entity framework data context. 

    here is draft of my class :

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    using System.Text.RegularExpressions;
    using System.Data.Metadata.Edm;
    using System.Data.Objects;
    using System.Data.Entity;
    using System.Data.Objects.DataClasses;
    using Microsoft.CSharp;
    using System.Reflection; 
    
    namespace UCbindingNav
    {
        public partial class BindingNav : UserControl
        {
            public BindingSource BindingSource { get; set; }
            public int Position { get; set; }
            public string SearchFeild{ get; set; }
    
    
            public BindingNav()
            { 
                InitializeComponent();
            }
    
            private void BindingNav_Load(object sender, EventArgs e)
            {
                positionItem.Text = (BindingSource.Position + 1).ToString();
                countItem.Text = "/ {" + BindingSource.Count + "}";
                firstItem.Enabled = false;
                previousItem.Enabled = false;
            }
    
    
    
            private void firstItem_Click(object sender, EventArgs e)
            {
                BindingSource.MoveFirst();
                positionItem.Text = (BindingSource.Position + 1).ToString();
                countItem.Text = "of {" + BindingSource.Count + "}";
                firstItem.Enabled = false;
                previousItem.Enabled = false;
                nextItem.Enabled = true;
                lastItem.Enabled = true;
            }
    
            private void previousItem_Click(object sender, EventArgs e)
            {
                BindingSource.MovePrevious();
                positionItem.Text = (BindingSource.Position + 1).ToString();
                countItem.Text = "of {" + BindingSource.Count + "}";
                if (BindingSource.Position == 0)
                {
                    firstItem.Enabled = false;
                    previousItem.Enabled = false;
                }
                else
                {
                    firstItem.Enabled = true;
                    previousItem.Enabled = true;
                }
                nextItem.Enabled = true;
                lastItem.Enabled = true;
    
            }
    
            private void nextItem_Click(object sender, EventArgs e)
            {
    
                this.Enabled = false;         
                Cursor = Cursors.WaitCursor;  
    
                BindingSource.MoveNext();
                positionItem.Text = (BindingSource.Position + 1).ToString();
                countItem.Text = "of {" + BindingSource.Count + "}";
                if ((BindingSource.Position + 1) == BindingSource.Count)
                {
                    nextItem.Enabled = false;
                    lastItem.Enabled = false;
                }
                else
                {
                    nextItem.Enabled = true;
                    lastItem.Enabled = true;
                }
                firstItem.Enabled = true;
                previousItem.Enabled = true;
    
                Cursor = Cursors.Default;
                this.Enabled = true;
            }
    
            private void lastItem_Click(object sender, EventArgs e)
            {
                BindingSource.MoveLast();
                positionItem.Text = (BindingSource.Position + 1).ToString();
                countItem.Text = "of {" + BindingSource.Count + "}";
                nextItem.Enabled = false;
                lastItem.Enabled = false;
                firstItem.Enabled = true;
                previousItem.Enabled = true;
            }
    
            private void addItem_Click(object sender, EventArgs e)
            {
                BindingSource.AddNew();
                positionItem.Text = (BindingSource.Position + 1).ToString();
                countItem.Text = "of {" + BindingSource.Count + "}";
                Position = BindingSource.Position + 1;
                nextItem.Enabled = false;
                lastItem.Enabled = false;
                firstItem.Enabled = true;
                previousItem.Enabled = true;
            }
    
            private void deleteItem_Click(object sender, EventArgs e)
            {
                BindingSource.RemoveCurrent();
                positionItem.Text = (BindingSource.Position + 1).ToString();
                countItem.Text = "of {" + BindingSource.Count + "}";
                Position = BindingSource.Position;
            }
    
            private void positionItem_KeyPress(object sender, KeyPressEventArgs e)
            {
                Regex expr = new Regex(@"^[1-9]{1}[0-9]*$");
                if (expr.IsMatch(positionItem.Text) && (e.KeyChar == '\r'))
                {
                    if (Convert.ToInt32(positionItem.Text) > 0 && Convert.ToInt32(positionItem.Text) <= BindingSource.Count)
                    {
                        BindingSource.Position = Convert.ToInt32(positionItem.Text) - 1;
    
                        if (Convert.ToInt32(positionItem.Text) == 1)
                        {
                            firstItem.Enabled = false;
                            previousItem.Enabled = false;
                            nextItem.Enabled = true;
                            lastItem.Enabled = true;
                        }
                        else if (Convert.ToInt32(positionItem.Text) == BindingSource.Count)
                        {
                            firstItem.Enabled = true;
                            previousItem.Enabled = true;
                            nextItem.Enabled = false;
                            lastItem.Enabled = false;
                        }
                        else
                        {
                            firstItem.Enabled = true;
                            previousItem.Enabled = true;
                            nextItem.Enabled = true;
                            lastItem.Enabled = true;
                        }
                    }
    
                }
    
            }
    
            private void saveItem_Click(object sender, EventArgs e)
            {
                Cursor c = ParentForm.Cursor;
                try
                {
                    ParentForm.Cursor = Cursors.WaitCursor;
                    ParentForm.Validate();
                    ((System.Data.Objects.ObjectQuery)BindingSource.DataSource).Context.SaveChanges();
                }
                finally
                {
                    ParentForm.Cursor = c;
                }
            }
    
            private void searchValue_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == '\r') search_Click(this, null);
            }
    
            private void search_Click(object sender, EventArgs e)
            {
    // Where i'm stuck in !!!            
            }
        }
    }
    

    Thursday, May 2, 2013 5:05 PM

Answers