narsil/Tanshu.Accounts.PointOfSale/Products/ProductGroupListForm.cs

101 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Repository;
namespace Tanshu.Accounts.PointOfSale
{
public partial class ProductGroupListForm : Form
{
private IList<ProductGroup> _list;
public ProductGroupListForm()
{
InitializeComponent();
dgvProductGroups.CellClick += new DataGridViewCellEventHandler(dgvProductGroups_CellClick);
}
private void btnAdd_Click(object sender, EventArgs e)
{
using (var frm = new ProductGroupForm(null))
frm.ShowDialog();
using (var bi = new ProductGroupBI())
_list = bi.List();
bsList.DataSource = _list;
}
private void ProductGroupListForm_Load(object sender, EventArgs e)
{
using (var bi = new ProductGroupBI())
_list = bi.List();
bsList.DataSource = _list;
}
private void btnEdit_Click(object sender, EventArgs e)
{
var id = ((ProductGroup)bsList.Current).ProductGroupID;
using (var frm = new ProductGroupForm(id))
frm.ShowDialog();
using (var bi = new ProductGroupBI())
_list = bi.List();
bsList.DataSource = _list;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void dgvProductGroups_CellClick(object sender, DataGridViewCellEventArgs e)
{
// Ignore clicks that are not on button cells.
if (e.RowIndex < 0)
return;
// Ignore clicks that are not on button cells.
if (e.ColumnIndex == dgvProductGroups.Columns["moveUp"].Index)
{
var productGroup = dgvProductGroups.Rows[e.RowIndex].DataBoundItem as ProductGroup;
var index = _list.IndexOf(productGroup);
if (index == 0)
return;
var previousProduct = _list[index - 1];
_list.RemoveAt(index);
_list.Insert(index - 1, productGroup);
bsList.DataSource = _list;
bsList.ResetBindings(false);
bsList.CurrencyManager.Position -= 1;
}
if (e.ColumnIndex == dgvProductGroups.Columns["moveDown"].Index)
{
var productGroup = dgvProductGroups.Rows[e.RowIndex].DataBoundItem as ProductGroup;
var index = _list.IndexOf(productGroup);
if (index == _list.Count + 1)
return;
var nextProduct = _list[index + 1];
_list.RemoveAt(index);
_list.Insert(index + 1, productGroup);
bsList.DataSource = _list;
bsList.ResetBindings(false);
bsList.CurrencyManager.Position += 1;
}
}
private void btnSave_Click(object sender, EventArgs e)
{
using (var bi = new ProductGroupBI())
{
bi.UpdateSortOrder(_list);
bi.SaveChanges();
_list = bi.List();
bsList.DataSource = _list;
}
}
}
}