关闭

界面对应字段,绑定数据到gridview 显示 及删除功能 窗体事件 分页及键

标签: 绑定数据
257人阅读 评论(0) 收藏 举报
分类:
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 DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar.Rendering;
using DrugSaleSystem.Model;
using DrugSaleSystem.Components;
using DrugSaleSystem.DAL;

namespace DrugSaleSystem.ManagementCenter
{
    public partial class frmPurchaseCategory : Form
    {
        #region 私有变量
        private int iCurrPageIndex = 1;
        private int iPageCount = 1;
        private int iTotalRecord = 0;
        private int iPageSize = 30;
        #endregion
        private static frmPurchaseCategory _Instance = null;


        public frmPurchaseCategory()
        {
            InitializeComponent();
            _Instance = this;
        }

        public static frmPurchaseCategory Instance
        {
            get
            {
                //第一次使用,如果没有实例,创建一个 
                if (_Instance == null)
                {
                    _Instance = new frmPurchaseCategory();
                }
                return _Instance;
            }
        }
        /// <summary>
        /// 初始化
        /// </summary>
        public void InitData()
        {
            frmMain.Instance.StyleType = ConstInfo.const_Stype;
            LoadImgInit();
            PurchaseCategoryColumn();       
            //DrugCategoryStatusInit();
            PurchaseCategoryInit();
            SetUserPermission();

        }
        #region 自定义函数
        public void LoadImgInit()
        {
            pbLogo.Image = Image.FromFile(Servers.MapPath(@"\Resources\Images\ManagementCenter\Public\png\80_80\PurchaseCategory.png"));
        }

        /// <summary>
        /// 设置权限
        /// </summary>
        private void SetUserPermission()
        {
            //tsmiActiveDrugInsert.Enabled = (tsmiActiveDrugInsert.Enabled && ComFuc.CheckUserPermissionsByPerName("ActiveDrugInsertPanel")) ? true : false;
            //tsmiActiveDrugRemove.Enabled = (tsmiActiveDrugRemove.Enabled && ComFuc.CheckUserPermissionsByPerName("ActiveDrugRemovePanel")) ? true : false;
        }
        private void PurchaseCategoryColumn()
        {
            dgvPurchaseCategoryList.Columns.Clear();
            BindDataGridViewColumns(dgvPurchaseCategoryList, 0, "SortDescriptionID", "自增编号", 150, false, true);
            BindDataGridViewColumns(dgvPurchaseCategoryList, 1, "SortDescriptionCode", "分类记录编码", 150, true, true);
            BindDataGridViewColumns(dgvPurchaseCategoryList, 2, "SortObject", "分类对象", 150, true, true);      
            BindDataGridViewColumns(dgvPurchaseCategoryList, 3, "SortName", "分类名称", 120, true, true);
            BindDataGridViewColumns(dgvPurchaseCategoryList, 4, "SpellCode", "拼音码", 120, true, true);
            BindDataGridViewColumns(dgvPurchaseCategoryList, 5, "Description", "分类说明描述", 180, true, true);
            BindDataGridViewColumns(dgvPurchaseCategoryList, 6, "Operator", "操作员", 120, true, true);
            BindDataGridViewColumns(dgvPurchaseCategoryList, 7, "OperateDate", "操作时间", 180, true, true);
        }
        private void BindDataGridViewColumns(DataGridView dgv, int index, string clumnname, string clumnmsg, int width, bool isvisible, bool isReadOnly)
        {
            dgv.Columns.Add(clumnname, clumnmsg);
            dgv.Columns[index].DataPropertyName = clumnname;
            dgv.Columns[index].Width = width;
            dgv.Columns[index].ReadOnly = isReadOnly;
            dgv.Columns[index].Visible = isvisible;
        }

        public void PurchaseCategoryInit()
        {
            dgvPurchaseCategoryList.Rows.Clear();
            int SortDescriptionCode = 0;
            if (txbSortDescriptionCode.Text == "")
            {
                SortDescriptionCode = 0;
            }
            else
            {
                SortDescriptionCode = Convert.ToInt32(txbSortDescriptionCode.Text);
            }
            int SortObject = 2;                  
            string sortName = txbSpellCode.Text;
            string spellCode = txbSortName.Text;
            string Description = "";
            int Status = 0;
            PageSetInfo psi = SortDescription.Get(SortDescriptionCode, SortObject, sortName, spellCode, Description, Status, iPageSize, iCurrPageIndex);
            iTotalRecord = psi.TotalRecords;
            if (iTotalRecord % iPageSize == 0)
            {
                iPageCount = iTotalRecord / iPageSize;
            }
            else
            {
                iPageCount = iTotalRecord / iPageSize + 1;
            }
            lbPageMsg.Text = string.Format("当前第{0}页,总共{1}条记录,总共{2}页", iCurrPageIndex, iTotalRecord, iPageCount);
            foreach (SortDescriptionInfo info in psi.SortDescriptionInfo)
            {
                dgvPurchaseCategoryList.Rows.Add(new object[]{
                    info.SortDescriptionID,
					info.SortDescriptionCode,                 
                    SortObjectAttributeType.LoadSortObjectAttribute("2"),					
					info.SortName,
                    info.SpellCode,
					info.Description,
					info.Status,
					info.Operator,
					info.OperateDate,
					info.Remark

                 });
            }
        } 
        public void EditPara()
        {
            if (dgvPurchaseCategoryList.SelectedRows == null || dgvPurchaseCategoryList.SelectedRows.Count == 0)
            {
                //显示对话框
                DevComponents.DotNetBar.MessageBoxEx.Show("请选择要编辑的类别!");
                return;
            }
            frmPurchaseCategoryEdit.Instance.iSortDescriptionID = Convert.ToInt32(dgvPurchaseCategoryList.SelectedRows[0].Cells[0].Value);
            frmPurchaseCategoryEdit.Instance.InitData();
            frmPurchaseCategoryEdit.Instance.ShowDialog();
        }
        public void InsertPara()
        {
            frmPurchaseCategoryEdit.Instance.iSortDescriptionID = 0;
            frmPurchaseCategoryEdit.Instance.InitData();
            frmPurchaseCategoryEdit.Instance.ShowDialog();
        }
        public void RemovePara()
        {
            if (dgvPurchaseCategoryList.SelectedRows == null || dgvPurchaseCategoryList.SelectedRows.Count == 0)
            {
                //显示对话框
                DevComponents.DotNetBar.MessageBoxEx.Show("请选择要删除的类别!");
                return;
            }
            if (MessageBox.Show(string.Format("确认要删除父类别[{0}]吗?删除后将不能恢复!", dgvPurchaseCategoryList.SelectedRows[0].Cells[2].Value), "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                int iSortDescriptionID = Convert.ToInt32(dgvPurchaseCategoryList.SelectedRows[0].Cells[0].Value);
                if (SortDescription.Remove(SortDescription.GetBySortDescriptionID(iSortDescriptionID)) > 0)
                {
                    #region 记录日志
                    OperationLogInfo loginfo = new OperationLogInfo();
                    loginfo.OperatorNO = ConstInfo.const_UserCode.ToString();
                    loginfo.OperatorName = ConstInfo.const_UserTrueName;
                    loginfo.Body = string.Format("删除类别成功!类别编码:{0}", iSortDescriptionID);
                    loginfo.OperResult = "成功";
                    loginfo.Source = Clients.GetLocalIP();
                    loginfo.OperateDate = DateTime.Now;
                    OperationLog.Create(loginfo);
                    #endregion
                    iCurrPageIndex = 1;
                    PurchaseCategoryInit();
                    MessageBox.Show("删除成功!");
                }

            }
        }

        #endregion
        #region 窗体事件
        private void frmPurchaseCategory_Load(object sender, EventArgs e)
        {
            DataGridViewCellStyle dataGridViewCellStyle = new DataGridViewCellStyle();
            dataGridViewCellStyle.BackColor = System.Drawing.Color.AliceBlue;
            #region DateGridView样式
            dgvPurchaseCategoryList.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle;
            dgvPurchaseCategoryList.BackgroundColor = System.Drawing.Color.Beige;
            dgvPurchaseCategoryList.ColumnHeadersVisible = true;
            dgvPurchaseCategoryList.RowHeadersVisible = false;
            this.dgvPurchaseCategoryList.ColumnHeadersHeightSizeMode =
                System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvPurchaseCategoryList.SelectionMode =
                System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.dgvPurchaseCategoryList.AllowUserToOrderColumns = false;
            this.dgvPurchaseCategoryList.AllowUserToAddRows = false;
            this.dgvPurchaseCategoryList.AllowUserToDeleteRows = false;
            this.dgvPurchaseCategoryList.AllowUserToResizeRows = false;
            this.dgvPurchaseCategoryList.MultiSelect = false;
            dgvPurchaseCategoryList.BorderStyle = BorderStyle.FixedSingle;
            dgvPurchaseCategoryList.ScrollBars = ScrollBars.Both;
            dgvPurchaseCategoryList.AllowUserToResizeColumns = true;
            //dgvDiscountCategoryList.ColumnHeadersDefaultCellStyle.Font = new Font("宋体", 12, FontStyle.Bold);
            dgvPurchaseCategoryList.ColumnHeadersHeight = 25;
            dgvPurchaseCategoryList.RowTemplate.Height = 25;
            #endregion
        }

        private void btnFirstPage_Click(object sender, EventArgs e)
        {
            iCurrPageIndex = 1;
            PurchaseCategoryInit();
        }

        private void btnLastPage_Click(object sender, EventArgs e)
        {
            if (iCurrPageIndex > 1)
            {
                iCurrPageIndex = iCurrPageIndex - 1;
                PurchaseCategoryInit();
            }
            else
            {
                MessageBox.Show("已经是第一页了!");
            }
        }

        private void btnNextPage_Click(object sender, EventArgs e)
        {
            if (iCurrPageIndex < iPageCount)
            {
                iCurrPageIndex = iCurrPageIndex + 1;
                PurchaseCategoryInit();
            }
            else
            {
                MessageBox.Show("已经是最后一页了!");
            }
        }

        private void btnEndPage_Click(object sender, EventArgs e)
        {
            iCurrPageIndex = iPageCount;
            PurchaseCategoryInit();
        }

        private void btnGoPage_Click(object sender, EventArgs e)
        {
            int intPageIndex = 1;
            if (!int.TryParse(tbGoPageNum.Text, out intPageIndex))
            {
                MessageBox.Show("页码必须为数字!");
                tbGoPageNum.Focus();
                return;
            }
            iCurrPageIndex = intPageIndex;
            if (iCurrPageIndex > iPageCount)
            {
                MessageBox.Show("页码已经超过总页数了!");
                tbGoPageNum.Focus();
                return;
            }
            PurchaseCategoryInit();
        }


        private void dgvPurchaseCategoryList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            EditPara();
        }

        private void dgvPurchaseCategoryList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                EditPara();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            iCurrPageIndex = 1;
            PurchaseCategoryInit();
        }

        private void txbSortDescriptionCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                iCurrPageIndex = 1;
                PurchaseCategoryInit();
            }
        }

        private void txbSpellCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                iCurrPageIndex = 1;
                PurchaseCategoryInit();
            }
        }

        private void txbSortName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                iCurrPageIndex = 1;
                PurchaseCategoryInit();
            }
        }


        private void tsmiPurchaseCategoryEdit_Click(object sender, EventArgs e)
        {
            EditPara();
        }

        private void tsmiPurchaseCategoryInsert_Click(object sender, EventArgs e)
        {
            InsertPara();
        }

        private void tsmiPurchaseCategoryRemove_Click(object sender, EventArgs e)
        {
            RemovePara();
        }
        #endregion




    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8995次
    • 积分:333
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条