Just a Simple CRUD WPF

image

Here is the code

   1:  namespace SimpleCRUD
   2:  {
   3:      /// <summary>
   4:      /// Interaction logic for Window1.xaml
   5:      /// </summary>
   6:      /// 
   7:      public partial class Window1 : Window
   8:      {
   9:          IEnumerable<employee> employeecontainer;
  10:          CollectionView view;
  11:          public Window1()
  12:          {
  13:              InitializeComponent();
  14:          }
  15:   
  16:          private void btnexit_Click(object sender, RoutedEventArgs e)
  17:          {
  18:              this.Close();
  19:          }
  20:   
  21:          private void Window_Loaded(object sender, RoutedEventArgs e)
  22:          {
  23:              BindData();
  24:           }
  25:   
  26:          private void BindData()
  27:          {
  28:              using (MyTestDBDataContext datactx = new MyTestDBDataContext())
  29:              {
  30:                  employeecontainer = (from a in datactx.employees select a);
  31:                  this.DataContext = datactx.employees;
  32:                  view = (CollectionViewSource.GetDefaultView(employeecontainer) as CollectionView);
  33:                  view.MoveCurrentToFirst();
  34:                  stcRight.DataContext = view;
  35:   
  36:              }
  37:          }
  38:          private void ClearAllTextBox()
  39:          {
  40:              foreach (var item in stcRight.Children)
  41:              {
  42:                  if (item.GetType().Name == "TextBox")
  43:                      ((TextBox)item).Text = string.Empty;
  44:              }
  45:          }
  46:       
  47:          private void cmdadd_Click(object sender, RoutedEventArgs e)
  48:          {
  49:              if (cmdadd.Content.ToString() == "Add")
  50:              {
  51:              ClearAllTextBox();
  52:              cmdadd.Content = "Update";
  53:              sbaritem.Content = String.Empty;
  54:              txtid.Background = new SolidColorBrush(Colors.Gray);
  55:              txtname.Focus();
  56:              }
  57:              else
  58:              {
  59:                  employee tmp = new employee();             
  60:                  tmp.Name = txtname.Text;
  61:                  tmp.Address = txtaddress.Text;
  62:                  using (MyTestDBDataContext datactx = new MyTestDBDataContext())
  63:                  {
  64:                      datactx.employees.InsertOnSubmit(tmp);
  65:                      try
  66:                      {
  67:                          datactx.SubmitChanges();
  68:                          sbaritem.Content = "Success Insert 1 Record";
  69:                          BindData();
  70:                          view.MoveCurrentToLast();
  71:                      }
  72:                      catch (Exception ex)
  73:                      {
  74:                          MessageBox.Show(ex.Message);
  75:                      }
  76:                  }               
  77:                  cmdadd.Content = "Add";
  78:                  txtid.Background = new SolidColorBrush(Colors.White);
  79:              
  80:              }
  81:          }
  82:   
  83:          private void btnmovefirst_Click(object sender, RoutedEventArgs e)
  84:          {
  85:              view.MoveCurrentToFirst();
  86:          }
  87:   
  88:          private void btnprevious_Click(object sender, RoutedEventArgs e)
  89:          {
  90:              if(view.CurrentPosition>0)
  91:              view.MoveCurrentToPrevious();
  92:          }
  93:   
  94:          private void btnmovenext_Click(object sender, RoutedEventArgs e)
  95:          {
  96:              if (view.CurrentPosition < view.Count - 1)
  97:              {
  98:                  view.MoveCurrentToNext();
  99:              }
 100:          }
 101:   
 102:          private void btndelete_Click(object sender, RoutedEventArgs e)
 103:          {
 104:              if(view.CurrentPosition>-1)
 105:              {
 106:                  using (MyTestDBDataContext datactx = new MyTestDBDataContext())
 107:                  {
 108:                      datactx.employees.Attach((employee)view.CurrentItem);
 109:                      datactx.employees.DeleteOnSubmit((employee)view.CurrentItem);
 110:                      datactx.SubmitChanges();
 111:                      BindData();
 112:                  }
 113:              }
 114:          }
 115:   
 116:          private void btnmovelast_Click(object sender, RoutedEventArgs e)
 117:          {
 118:              view.MoveCurrentToLast();
 119:          }
 120:   
 121:          private void btncancel_Click(object sender, RoutedEventArgs e)
 122:          {
 123:              view.MoveCurrentToFirst();
 124:              cmdadd.Content = "Add";
 125:          }
 126:   
 127:       
 128:   
 129:      }
 130:  }
Share this post: | | | |
Published Thursday, October 30, 2008 11:02 AM by cipto
Filed under:

Comments

No Comments