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

原创 2016年08月31日 16:50:49
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




    }
}

相关文章推荐

gridview 绑定数据.实现update、delete、insert into、分页功能

  • lx_nq
  • lx_nq
  • 2011年07月24日 21:59
  • 178

把集合绑定到数据控件上并实现分页功能

前些时候学习asp.net三层架构的时候,想把集合绑定到listview上。但是怎么也想不出来,很是苦恼网上貌似也找不到比较合适的答案,今天心血来潮问了下身边的同事。通过同事的细心讲解我才恍然大悟原来...

一个gridview数据绑定和分页

  • 2010年02月05日 17:20
  • 1KB
  • 下载

asp.net 站内搜索功能的实现及利用datatable绑定数据并进行分页

最近在做公司网站的时候,用到了站内搜索,之前没做过,经过几天的研究和百度,GOOGLE,基本实现了我想达到的效果,在这里记录一下,写的比较乱,有时间再整理一下 首先先看图, 要实现上图的搜索功能,...
  • zx_001
  • zx_001
  • 2015年09月16日 11:21
  • 940

菜鸟学Asp.Net——GridView控件之RowDataBind事件中如何获取已绑定的数据

初看标题,貌似没什么大不了的,但是当小弟我去实践的时候却发现,事实并非那么简单! 首先,小弟用的是SqlDataSourse绑定到GridView的, 我想实现的效果是:如果状态中,半数以上是...
  • kxLoveH
  • kxLoveH
  • 2012年05月10日 23:43
  • 5353

GridView自定义列数据绑定,和自定义颁功能

using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste...

操作datatable进行分页,绑定repeqter或者gridView

今天上午研究一个多小时了终于出来了。前台页面                    ...

操作datatable进行分页,绑定repeqter或者gridView

前台页面http://www.w3.org/1999/xhtml">                                                        标题        ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:界面对应字段,绑定数据到gridview 显示 及删除功能 窗体事件 分页及键
举报原因:
原因补充:

(最多只允许输入30个字)