-1-
-2-
using customerproj.db;
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace customerproj
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
POSTutEntities db = new POSTutEntities();
string imagePath = "";
int i=0;
Customer cust;
public Form1()
{
InitializeComponent();
//POSTutEntities db = new POSTutEntities();
// gridControl1.DataSource = db.Customers.ToList();
this.customerTableAdapter.Fill(this.pOSTutDataSet.Customer);
gridView1.OptionsBehavior.Editable = false;
gridView1.Columns["id"].Visible = false;
gridView1.Columns["Name"].Caption = "الاسم";
//==============================================
txtid.DataBindings.Add("text", bindingSource1, "id");
txtName.DataBindings.Add("text", bindingSource1, "Name");
txtAddress.DataBindings.Add("text", bindingSource1, "Address");
txtEmail.DataBindings.Add("text", bindingSource1, "Email");
txtCompany.DataBindings.Add("text", bindingSource1, "Company");
txtPhone.DataBindings.Add("text", bindingSource1, "Phone");
txtNotes.DataBindings.Add("text", bindingSource1, "Notes");
isActive.DataBindings.Add("Checked", bindingSource1, "isActive");
pictureBox1.DataBindings.Add("ImageLocation", bindingSource1, "Image");
//===================
}
private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
GetData();
}
void refresh1()
{
//POSTutEntities db = new POSTutEntities();
// gridControl1.DataSource = db.Customers.ToList();
this.customerTableAdapter.Fill(this.pOSTutDataSet.Customer);
gridView1.OptionsBehavior.Editable = false;
gridView1.Columns["id"].Visible = false;
gridView1.Columns["Name"].Caption = "الاسم";
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'pOSTutDataSet.Customer' table. You can move, or remove it, as needed.
this.customerTableAdapter.Fill(this.pOSTutDataSet.Customer);
refresh1();
}
void GetData()
{
txtName.Text = cust.Name;
txtAddress.Text = cust.Address;
txtNotes.Text = cust.Notes;
txtPhone.Text = cust.Phone;
txtCompany.Text = cust.Company;
txtEmail.Text = cust.Email;
isActive.Checked = (bool)cust.IsActive;
pictureBox1.ImageLocation = cust.Image;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
imagePath = dialog.FileName;
pictureBox1.ImageLocation = dialog.FileName;
}
}
void Save()
{
// in case of no data
if (cust == null)
{
cust = new Customer();
db.Customers.Add(cust);
db.SaveChanges();
}
cust.Name = txtName.Text;
if (txtName.Text.Trim() == string.Empty)
{
txtName.ErrorText = "Plz enter Name of Customer";
return;
}
cust.Address = txtAddress.Text;
cust.Notes = txtNotes.Text;
cust.Phone = txtPhone.Text;
cust.Company = txtCompany.Text;
cust.Email = txtEmail.Text;
cust.IsActive = isActive.Checked;
if (imagePath != "")
{
// string destPath = Environment.CurrentDirectory + "\\images\\products\\" + p.Id + ".jpg";
string destPath = "images\\Customers\\" + cust.id + ".jpg";
File.Copy(imagePath, destPath, true);
cust.Image = destPath;
}
db.SaveChanges();
XtraMessageBox.Show("Done Successfully");
// cust.Image= pictureBox1.ImageLocation ;
}
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Save();
refresh1();
}
private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
max = db.Customers.Min(x => x.id);
cust = db.Customers.Find(max); // Last get error in no data
if (cust == null) return; // in case of no data
GetData();
}
void newData()
{
Customer c = new Customer();
c.Name = txtName.Text;
c.Address = txtAddress.Text;
c.Notes = txtNotes.Text;
c.Phone = txtPhone.Text;
c.Company = txtCompany.Text;
c.Email = txtEmail.Text;
c.IsActive = isActive.Checked;
db.Customers.Add(c);
db.SaveChanges();
if (imagePath != "")
{
// string destPath = Environment.CurrentDirectory + "\\images\\products\\" + p.Id + ".jpg";
string destPath = "images\\Customers\\" + c.id + ".jpg";
File.Copy(imagePath, destPath, true);
c.Image = destPath;
db.SaveChanges();
}
XtraMessageBox.Show("Submitted Successfully");
refresh1();
}
private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Clear();
cust = new Customer();
db.Customers.Add(cust);
// refresh1();
}
private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
DelData();
refresh1();
}
void Clear()
{
txtName.Text =
txtAddress.Text
= txtNotes.Text
= txtPhone.Text
= txtCompany.Text
= txtEmail.Text = "";
isActive.Checked = false;
pictureBox1.ImageLocation = "";
}
void DelData()
{
if (XtraMessageBox.Show("Are You Sure to Delete this Record ?", "EF CRUD Operation", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
//Product x = db.Customers.Find(i);
//db.Products.Remove(x);
// db.Products.Attach(cust);
//======= Or
// db.Customers.Attach(cust);
db.Customers.Remove(cust);
db.SaveChanges();
Clear();
XtraMessageBox.Show("Deleted Successfully");
}
}
private void bindingNavigator1_BindingContextChanged(object sender, EventArgs e)
{
}
private void bindingNavigator1_RefreshItems(object sender, EventArgs e)
{
}
private void barButtonItem6_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Clear();
}
int max = 0;
private void barButtonItem7_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
max = db.Customers.Max(x => x.id);
cust = db.Customers.Find(max); // Last get error in no data
if (cust == null) return; // in case of no data
GetData();
}
private void barButtonItem8_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
//max++;
//cust = db.Customers.Find(max); // Last get error in no data
//if (cust == null) return; // in case of no data
//GetData();
}
private void barButtonItem9_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
//max--;
//cust = db.Customers.Find(max); // Last get error in no data
//if (cust == null) return; // in case of no data
//GetData();
}
private void gridControl1_DoubleClick(object sender, EventArgs e)
{
gridToForm();
}
void gridToForm()
{
int pk = 0;
pk = Convert.ToInt32(gridView1.GetFocusedRowCellValue("id"));
try
{
cust = db.Customers.SingleOrDefault(x => x.id == pk);
txtName.Text = cust.Name;
txtAddress.Text = cust.Address;
txtNotes.Text = cust.Notes;
txtPhone.Text = cust.Phone;
txtCompany.Text = cust.Company;
txtEmail.Text = cust.Email;
isActive.Checked = (bool)cust.IsActive;
pictureBox1.ImageLocation = cust.Image;
}
catch
{
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)
{
Save();
}
}
private void barButtonItem10_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
refresh1();
}
private void gridControl1_CursorChanged(object sender, EventArgs e)
{
}
private void gridControl1_ProcessGridKey(object sender, KeyEventArgs e)
{
}
private void bindingNavigator1_Click(object sender, EventArgs e)
{
}
private void txtid_TextChanged(object sender, EventArgs e)
{
}
void pointtorecord()
{
int pk = Convert.ToInt32(txtid.Text.Trim().ToString());
// MessageBox.Show(pk.ToString());
try
{
cust = db.Customers.SingleOrDefault(x => x.id == pk);
txtName.Text = cust.Name;
txtAddress.Text = cust.Address;
txtNotes.Text = cust.Notes;
txtPhone.Text = cust.Phone;
txtCompany.Text = cust.Company;
txtEmail.Text = cust.Email;
isActive.Checked = (bool)cust.IsActive;
pictureBox1.ImageLocation = cust.Image;
}
catch
{
}
//cust = db.Customers.SingleOrDefault(x=>x.id==i);
}
private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
{
pointtorecord();
}
private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
{
pointtorecord();
}
private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
{
pointtorecord();
}
private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
{
pointtorecord();
}
private void bindingNavigatorPositionItem_Click(object sender, EventArgs e)
{
pointtorecord();
}
}
}
============
-3-
-4-