PopedomManage.cs
上传用户:ksd66jhda
上传日期:2013-04-03
资源大小:234k
文件大小:10k
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- namespace 进销存管理系统
- {
- /// <summary>
- /// PopedomManage 的摘要说明。
- /// </summary>
- public class PopedomManage : System.Windows.Forms.Form
- {
- //设置数据库连接和适配器
- private DataSet ds = new DataSet();
- private LinkDataBase link = new LinkDataBase();
- private string sendTableName = "用户清单";
- private string sendStrSQL = "SELECT * from 用户清单 where 姓名<>'sys'";
- private DataTable popedomDataTable = new DataTable();
- private bool blInitial = true;
- private System.Windows.Forms.GroupBox groupBox1;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.CheckedListBox chkLst_Priority;
- private System.Windows.Forms.DataGrid dgrd_Popedom;
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.Container components = null;
- public PopedomManage()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if(components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows 窗体设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.groupBox1 = new System.Windows.Forms.GroupBox();
- this.chkLst_Priority = new System.Windows.Forms.CheckedListBox();
- this.label2 = new System.Windows.Forms.Label();
- this.label1 = new System.Windows.Forms.Label();
- this.dgrd_Popedom = new System.Windows.Forms.DataGrid();
- this.groupBox1.SuspendLayout();
- ((System.ComponentModel.ISupportInitialize)(this.dgrd_Popedom)).BeginInit();
- this.SuspendLayout();
- //
- // groupBox1
- //
- this.groupBox1.Controls.Add(this.chkLst_Priority);
- this.groupBox1.Controls.Add(this.label2);
- this.groupBox1.Controls.Add(this.label1);
- this.groupBox1.Controls.Add(this.dgrd_Popedom);
- this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
- this.groupBox1.Location = new System.Drawing.Point(0, 0);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(648, 357);
- this.groupBox1.TabIndex = 0;
- this.groupBox1.TabStop = false;
- //
- // chkLst_Priority
- //
- this.chkLst_Priority.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
- | System.Windows.Forms.AnchorStyles.Right)));
- this.chkLst_Priority.CheckOnClick = true;
- this.chkLst_Priority.Items.AddRange(new object[] {
- "商品资料维护",
- "供货商资料维护",
- "客户资料维护",
- "进货单",
- "销售单",
- "库存查询",
- "权限管理"});
- this.chkLst_Priority.Location = new System.Drawing.Point(456, 52);
- this.chkLst_Priority.Name = "chkLst_Priority";
- this.chkLst_Priority.Size = new System.Drawing.Size(172, 276);
- this.chkLst_Priority.TabIndex = 2;
- this.chkLst_Priority.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.chkLst_Priority_ItemCheck);
- //
- // label2
- //
- this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
- this.label2.Location = new System.Drawing.Point(450, 28);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(100, 16);
- this.label2.TabIndex = 5;
- this.label2.Text = "权限:";
- //
- // label1
- //
- this.label1.Location = new System.Drawing.Point(33, 28);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(100, 16);
- this.label1.TabIndex = 4;
- this.label1.Text = "用户:";
- //
- // dgrd_Popedom
- //
- this.dgrd_Popedom.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
- | System.Windows.Forms.AnchorStyles.Left)
- | System.Windows.Forms.AnchorStyles.Right)));
- this.dgrd_Popedom.CaptionVisible = false;
- this.dgrd_Popedom.DataMember = "";
- this.dgrd_Popedom.HeaderForeColor = System.Drawing.SystemColors.ControlText;
- this.dgrd_Popedom.Location = new System.Drawing.Point(26, 52);
- this.dgrd_Popedom.Name = "dgrd_Popedom";
- this.dgrd_Popedom.ReadOnly = true;
- this.dgrd_Popedom.Size = new System.Drawing.Size(412, 274);
- this.dgrd_Popedom.TabIndex = 1;
- this.dgrd_Popedom.MouseUp += new System.Windows.Forms.MouseEventHandler(this.dgrd_Popedom_MouseUp);
- //
- // PopedomManage
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(648, 357);
- this.Controls.Add(this.groupBox1);
- this.Name = "PopedomManage";
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- this.Text = "【权限管理】";
- this.Load += new System.EventHandler(this.PopedomManage_Load);
- this.groupBox1.ResumeLayout(false);
- ((System.ComponentModel.ISupportInitialize)(this.dgrd_Popedom)).EndInit();
- this.ResumeLayout(false);
- }
- #endregion
- //---------窗体创建时,将用户的权限数据读入并显示-----------
- private void PopedomManage_Load(object sender, System.EventArgs e)
- {
- this.ds = this.link.SelectDataBase(sendStrSQL,sendTableName);
- this.dgrd_Popedom.DataSource = ds.Tables[0];
- this.DataGridStateControl();
- this.dgrd_Popedom.Select(0);//窗体生成后选中第一行
- string selectedUserID = this.ds.Tables[0].Rows[0][0].ToString();//默认选中用户清单中的第一个用户
- string tempStrSQL = "select 权限名称 from 权限清单 " + " where 用户编号 = '" + selectedUserID + "'";
- popedomDataTable = link.SelectDataBase(tempStrSQL);//读入该用户的权限
- for (int i=0;i<popedomDataTable.Rows.Count;i++)//根据权限设置ListBox
- {
- for (int j=0;j<this.chkLst_Priority.Items.Count;j++)
- {
- if (this.chkLst_Priority.Items[j].ToString().Trim() == popedomDataTable.Rows[i][0].ToString().Trim())
- {
- this.chkLst_Priority.SetItemChecked(j,true);
- }
- }
- }
- }
- //-------------设置显示用户信息的表---------------
- private void DataGridStateControl()
- {
- DataGridTableStyle ts = new DataGridTableStyle();
- DataGridNoActiveCellColumn aColumnTextColumn;//设置dataGrid格式
- ts.AlternatingBackColor = Color.LightGray;
- ts.MappingName = this.ds.Tables[0].TableName;
- ts.AllowSorting = false;
- int numCols = this.ds.Tables[0].Columns.Count;
- for (int i = 0;i< numCols;i++)
- {
- aColumnTextColumn = new DataGridNoActiveCellColumn();
- aColumnTextColumn.MappingName = this.ds.Tables[0].Columns[i].ColumnName;
- aColumnTextColumn.HeaderText = this.ds.Tables[0].Columns[i].ColumnName;
- aColumnTextColumn.NullText = "";
- aColumnTextColumn.Format = "F";
- ts.GridColumnStyles.Add(aColumnTextColumn);
- }
- this.dgrd_Popedom.TableStyles.Add(ts);
- }
-
- //-------------改变用户的权限,并向数据库中提交----------------
- private void chkLst_Priority_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
- {
- //CheckedListBox控件中的Item的CheckedState属性是由点击CheckedListBox控件而改变时才响应该事件
- if (blInitial == false)
- {
- if (e.CurrentValue.ToString() == "Unchecked")
- {
- try
- {
- string strUserName = this.dgrd_Popedom[this.dgrd_Popedom.CurrentCell.RowNumber,0].ToString().Trim();
- string strPopedom = this.chkLst_Priority.SelectedItem.ToString();
- string tempSendStrSQL = "insert 权限清单 (用户编号,权限名称) values ('" + strUserName +"','"+ strPopedom +"')";
- this.link.UpdateDataBase(tempSendStrSQL);
- }
- catch
- {
- MessageBox.Show("数据库中的权限修改出错,请重试!","信息");
- }
- }
- else if (e.CurrentValue.ToString() == "Checked")
- {
- try
- {
- string strUserName = this.dgrd_Popedom[this.dgrd_Popedom.CurrentCell.RowNumber,0].ToString().Trim();
- string strPopedom = this.chkLst_Priority.SelectedItem.ToString();
- string tempSendStrSQL = "delete from 权限清单 where (用户编号 = '" + strUserName +"'" +
- " and 权限名称 = '" + strPopedom + "')";
- this.link.UpdateDataBase(tempSendStrSQL);
- }
- catch
- {
- MessageBox.Show("数据库中的权限修改出错!","信息");
- }
- }
- }
- }
- private void dgrd_Popedom_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
- {
- if (this.dgrd_Popedom.CurrentCell.RowNumber >= this.ds.Tables[0].Rows.Count)//防止出现所选的用户不在数据表中的情况
- {
- return;
- }
- this.chkLst_Priority.Enabled = true;
- blInitial = true; //控制当【权限管理】窗体刚生成时和点击其他用户时使CheckedListBox控件中数据发生改变时不响应ItemCheck事件
- //以下代码实现的是当dataGrid中所选用户改变时,根据选中用户权限重新设置listBox的功能
- for (int i=0;i<this.chkLst_Priority.Items.Count;i++)//将listBox中所有权限设为未选中
- {
- this.chkLst_Priority.SetItemChecked(i,false);
- }
- int intRowNumber = this.dgrd_Popedom.CurrentCell.RowNumber;
- string selectedUserID = this.ds.Tables[0].Rows[intRowNumber][0].ToString();
- string tempStrSQL = "select 权限名称 from 权限清单 " + " where 用户编号 = '" + selectedUserID + "'";
- popedomDataTable = link.SelectDataBase(tempStrSQL);//查询所选用户权限
- for (int i=0;i<popedomDataTable.Rows.Count;i++)//重新设置listBox
- {
- for (int j=0;j<this.chkLst_Priority.Items.Count;j++)
- {
- if (this.chkLst_Priority.Items[j].ToString().Trim() == popedomDataTable.Rows[i][0].ToString().Trim())
- {
- this.chkLst_Priority.SetItemChecked(j,true);
- }
- }
- }
- blInitial = false;
- }
- }
- }