using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 进销存管理系统
{
public partial class PopedomManage : 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;
public PopedomManage()
{
InitializeComponent();
}
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();
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++)
{
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();
ts.AlternatingBackColor = Color.LightGray;
ts.MappingName = this.ds.Tables[0].TableName;
ts.AllowSorting = false;
int numCols = this.ds.Tables[0].Columns.Count;
this.dgrd_Popedom.TableStyles.Add(ts);
}
private void chkLst_Priority_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
{
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 temSendStrSQL = "insert 权限清单(用户编号,权限名称)values ('" + strUserName+"','" + strPopedom+"')";
this.link.UpdateDataBase(temSendStrSQL);
}
catch
{
MessageBox.Show("数据库中的权限修改出错,请重试!","信息");
}
}
else if (e.CurrentValue.ToString()=="Chencked")
{
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;
}
}
}