📄 popedom.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace test007
{
/// <summary>
/// popedom 的摘要说明。
/// </summary>
public class popedom : System.Windows.Forms.Form
{
private DataSet ds = new DataSet();
private LinkDataBase link = new LinkDataBase();
private string sendTableName = "manage";
private string sendStrSQL = "SELECT * from manage where 管理员帐号<>'1'";
private DataTable popedomDataTable = new DataTable();
private bool blInitial = true;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.CheckedListBox checkedListBox1;
private System.Windows.Forms.GroupBox groupBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public popedom()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.label2 = new System.Windows.Forms.Label();
this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 16);
this.label1.TabIndex = 0;
this.label1.Text = "管理员列表";
//
// dataGrid1
//
this.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.dataGrid1.CaptionVisible = false;
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(16, 40);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.ReadOnly = true;
this.dataGrid1.Size = new System.Drawing.Size(312, 432);
this.dataGrid1.TabIndex = 1;
this.dataGrid1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.dataGrid1_MouseUp);
//
// label2
//
this.label2.Location = new System.Drawing.Point(360, 16);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(208, 16);
this.label2.TabIndex = 2;
this.label2.Text = "权限设置";
//
// checkedListBox1
//
this.checkedListBox1.Items.AddRange(new object[] {
"客户管理",
"配送员管理",
"管理员维护",
"权限管理",
"订单总汇",
"待处理订单",
"未完成订单",
"已完成订单",
"未实施订单",
"运输路线设计",
"运输路线管理",
"货物跟踪",
"回执单维护"});
this.checkedListBox1.Location = new System.Drawing.Point(344, 40);
this.checkedListBox1.Name = "checkedListBox1";
this.checkedListBox1.Size = new System.Drawing.Size(280, 436);
this.checkedListBox1.TabIndex = 3;
this.checkedListBox1.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.checkedListBox1_ItemCheck);
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.label2,
this.dataGrid1,
this.checkedListBox1});
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(736, 496);
this.groupBox1.TabIndex = 4;
this.groupBox1.TabStop = false;
this.groupBox1.Enter += new System.EventHandler(this.groupBox1_Enter);
//
// popedom
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(648, 501);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.groupBox1});
this.Name = "popedom";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "权限管理";
this.Load += new System.EventHandler(this.popedom_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void popedom_Load(object sender, System.EventArgs e)
{
this.ds = this.link.SelectDataBase(sendStrSQL,sendTableName);
this.dataGrid1.DataSource = ds.Tables[0];
this.DataGridStateControl();
this.dataGrid1.Select(0);//窗体生成后选中第一行
string selectedUserID = this.ds.Tables[0].Rows[0][0].ToString();//默认选中用户清单中的第一个用户
string tempStrSQL = "select 权限名称 from popedom " + " where 管理员帐号 = '" + selectedUserID + "'";
popedomDataTable = link.SelectDataBase(tempStrSQL);//读入该用户的权限
for (int i=0;i<popedomDataTable.Rows.Count;i++)//根据权限设置ListBox
{
for (int j=0;j<this.checkedListBox1.Items.Count;j++)
{
if (this.checkedListBox1.Items[j].ToString().Trim() == popedomDataTable.Rows[i][0].ToString().Trim())
{
this.checkedListBox1.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.dataGrid1.TableStyles.Add(ts);
}
//-------------改变用户的权限,并向数据库中提交----------------
private void checkedListBox1_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
{
//CheckedListBox控件中的Item的CheckedState属性是由点击CheckedListBox控件而改变时才响应该事件
if (blInitial == false)
{
if (e.CurrentValue.ToString() == "Unchecked")
{
try
{
string strUserName = this.dataGrid1[this.dataGrid1.CurrentCell.RowNumber,0].ToString().Trim();
string strPopedom = this.checkedListBox1.SelectedItem.ToString();
string tempSendStrSQL = "insert popedom (管理员帐号,权限名称) values ('" + strUserName +"','"+ strPopedom +"')";
this.link.UpdateDataBase(tempSendStrSQL);
}
catch
{
MessageBox.Show("数据库中的权限修改出错,请重试!","信息");
}
}
else if (e.CurrentValue.ToString() == "Checked")
{
try
{
string strUserName = this.dataGrid1[this.dataGrid1.CurrentCell.RowNumber,0].ToString().Trim();
string strPopedom = this.checkedListBox1.SelectedItem.ToString();
string tempSendStrSQL = "delete from popedom where (管理员帐号 = '" + strUserName +"'" +
" and 权限名称 = '" + strPopedom + "')";
this.link.UpdateDataBase(tempSendStrSQL);
}
catch
{
MessageBox.Show("数据库中的权限修改出错!","信息");
}
}
}
}
private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (this.dataGrid1.CurrentCell.RowNumber >= this.ds.Tables[0].Rows.Count)//防止出现所选的用户不在数据表中的情况
{
return;
}
this.checkedListBox1.Enabled = true;
blInitial = true;//控制当【权限管理】窗体刚生成时和点击其他用户时使CheckedListBox控件中数据发生改变时不响应ItemCheck事件
//以下代码实现的是当dataGrid中所选用户改变时,根据选中用户权限重新设置listBox的功能
for (int i=0;i<this.checkedListBox1.Items.Count;i++)//将listBox中所有权限设为未选中
{
this.checkedListBox1.SetItemChecked(i,false);
}
int intRowNumber = this.dataGrid1.CurrentCell.RowNumber;
string selectedUserID = this.ds.Tables[0].Rows[intRowNumber][0].ToString();
string tempStrSQL = "select 权限名称 from popedom " + " where 管理员帐号 = '" + selectedUserID + "'";
popedomDataTable = link.SelectDataBase(tempStrSQL);//查询所选用户权限
for (int i=0;i<popedomDataTable.Rows.Count;i++)//重新设置listBox
{
for (int j=0;j<this.checkedListBox1.Items.Count;j++)
{
if (this.checkedListBox1.Items[j].ToString().Trim() == popedomDataTable.Rows[i][0].ToString().Trim())
{
this.checkedListBox1.SetItemChecked(j,true);
}
}
}
blInitial = false;
}
private void groupBox1_Enter(object sender, System.EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -