The Perfect Day

分享技术,编写未来

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataAccess.Data;

namespace CTERP
{
    
public partial class FrmUsersRoleInfo : Form
    {
        
public FrmUsersRoleInfo()
        {
            InitializeComponent();
        }
        LinkDataBase link 
= new LinkDataBase();
        DataSet dsMix 
= null;
        
string sqlRole = "select * from sy_dictrole";
        
string sqlUsers = @"SELECT checkFlag = cast(0 as int),id,userid,fullname,description,password,grade,effectdate,expirydate,updatedby,lastupdate FROM users";
        
string tbRole = "sy_dictrole", tbUsers = "users";      
        
private void FrmUsersRoleInfo_Load(object sender, EventArgs e)
        {
            link.CustomizeDataGridView(dgvRole);
            link.CustomizeDataGridView(dgvUsers);

            dsMix 
= link.ExecuteSQLDataSet(sqlRole, tbRole);
            dgvRole.DataSource 
= dsMix;
            dgvRole.DataMember 
= tbRole;

            dsMix 
= link.ExecuteSQLDataSet(sqlUsers, tbUsers);
            dgvUsers.DataSource 
= dsMix;
            dgvUsers.DataMember 
= tbUsers;

            
int i_roleid = int.Parse(dgvRole.Rows[0].Cells[0].Value.ToString());
            setCheckFlag(i_roleid);
        }

        
private void btnSave_Click(object sender, EventArgs e)
        {
            
if (dgvRole.Rows.Count < 0 || dgvRole.DataSource == nullreturn;
            
int currentRoleID = int.Parse(dgvRole.Rows[dgvRole.CurrentRow.Index].Cells[0].Value.ToString()); //得到当前行的roleid
            DataTable dtTmp = DbHelperSQL.Instance.GetDataTable("select * from sy_personvsrole where roleid=" + currentRoleID + "");
            
for (int i = 0; i < dgvUsers.Rows.Count; i++)
            {
                
int currentCheckFlag = int.Parse(dgvUsers.Rows[i].Cells[0].Value.ToString()); //得到当前行选择标记
                int currentPersonID = int.Parse(dgvUsers.Rows[i].Cells[1].Value.ToString()); //得到当前行用户ID
                int i_length = dtTmp.Select("personid=" + currentPersonID + "").Length; //从表sy_personvsrole中查找相同personid的记录
                if (currentCheckFlag == 0)
                {
                    
if (i_length > 0//没选中标记,在表sy_personvsrole中确实存在(表示:取消选中),则从表sy_personvsrole中删除
                    {
                        
string i_delete_sql = "delete from sy_personvsrole where personid=" + currentPersonID + "";
                        DbHelperSQL.Instance.ExcuteNonQuery(i_delete_sql);
                    }
                }
                
else
                {
                    
if (i_length == 0//已选中标记,在表sy_personvsrole中不存在(表示:新选中的),则在表sy_personvsrole中插入此条新记录
                    {
                        
string i_insert_sql = "insert into sy_personvsrole select " + currentPersonID + "," + currentRoleID + ",'" + string.Empty + "'";
                        DbHelperSQL.Instance.ExcuteNonQuery(i_insert_sql);
                    }
                }
            }
            MessageBox.Show(
"数据保存成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }

        
private void btnExit_Click(object sender, EventArgs e)
        {
            
this.Close();
        }

        
private void dgvRole_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
if (e.RowIndex != -1)
            {
                
for (int i = 0; i < dgvUsers.Rows.Count; i++)
                {
                    dgvUsers.Rows[i].Cells[
0].Value = 0;
                }
                
//dgvUsers.DataSource = DbHelperSQL.Instance.GetDataTable(sqlUsers);////到底谁的效率更好?
                int i_roleid = int.Parse(dgvRole.Rows[e.RowIndex].Cells[0].Value.ToString());
                setCheckFlag(i_roleid);
            }
        }

        
//设置复选框的状态
        private void setCheckFlag(int roleid)
        {
            
string sqlTmp = @"select personid from sy_personvsrole where roleid=" + roleid + "";
            DataTable dtTmp 
= DbHelperSQL.Instance.GetDataTable(sqlTmp);
            
for (int i = 0; i < dgvUsers.Rows.Count; i++)
            {
                
string str_id = dgvUsers.Rows[i].Cells[1].Value.ToString().Trim();
                
if (dtTmp.Select("personid=" + int.Parse(str_id) + "").Length > 0)
                {
                    dgvUsers.Rows[i].Cells[
0].Value = 1;//表示选中
                }
            }
        }
    }
}
posted on 2008-04-11 20:59  StephenJu  阅读(456)  评论(0编辑  收藏  举报