private void SaveUser()
        {
            try
            {
                DialogResult dr = MessageBox.Show("数据保存吗?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (dr == DialogResult.Yes)
                {

                    if (!CheckUser())
                        return;
                    User OneUser = new User();
                    OneUser.SetUserAttribute(this.txtUserID.Text.Trim(), this.textBox4.Text.Trim(), this.textBox2.Text.Trim(), this.txtPassword.Text, this.comboBox2.Text);
                    if (this.treeView1.SelectedNode == null)
                    {
                        this.Cursor = Cursors.WaitCursor;

                        DataTable dt = new DataTable();

                        dt = ds.Tables["S_User"];

                        DataRow dr_temp = dt.Rows[0];
                        dt.Clear();
                        dr_temp[0] = OneUser.UserID;

                        dr_temp[1] = OneUser.FamilyName;
                        dr_temp[2] = OneUser.Name;
                        dr_temp[3] = OneUser.Password;
                        dr_temp[4] = OneUser.RoleID;

                        dt.Rows.Add(dr_temp);
                        //添加
                        DBUser.Insert_User(dt);

                        HasChanged = false;
                        AllUser.Add(OneUser.UserID, OneUser);
                        AllUserID.Add(OneUser.UserID);
                        AllUserID.Sort();
                        AddValueToTreeView();
                        foreach (TreeNode td in this.treeView1.Nodes[1].Nodes)
                        {
                            if (td.Name == OneUser.UserID)
                                this.treeView1.SelectedNode = td;
                        }
                        CloneAllUser();
                        this.Cursor = Cursors.Default;

                        SQLExecute.Operate = "用户" + DBUser.CURR_USERID + "添加用户信息。";
                        SQLExecute.SaveOperate();
                    }
                    else
                    {
                        //更新

                        DataTable dt = new DataTable();

                        dt = ds.Tables["S_User"];

                        DataRow dr_temp = dt.Rows[0];
                        dt.Clear();
                        dr_temp[0] = OneUser.UserID;

                        dr_temp[1] = OneUser.FamilyName;
                        dr_temp[2] = OneUser.Name;
                        dr_temp[3] = OneUser.Password;
                        dr_temp[4] = OneUser.RoleID;

                        dt.Rows.Add(dr_temp);

                        DBUser.UpData_User(dt);
                        HasChanged = false;
                        AllUser[OneUser.UserID] = OneUser;
                        this.Cursor = Cursors.Default;
                        SQLExecute.Operate = "用户" + DBUser.CURR_USERID + "更新用户信息。";
                        SQLExecute.SaveOperate();
                    }
                    HasChanged = false;
                }
                else
                {
                    this.Cursor = Cursors.Default;
                    return;
                }
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                return;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                if (!HasChanged)
                    return;
                if (treeView1.SelectedNode.Parent.Name == "Role")
                {
                    #region 保存一个角色
                    try
                    {
                        DialogResult dr;
                        dr = MessageBox.Show("角色保存成功。", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                        UsePurview p = null;

                        if (dr == DialogResult.Yes)
                        {

                            this.Cursor = Cursors.WaitCursor;
                            p = AllRole[this.treeView1.SelectedNode.Name];
                            p.RoleName = this.textBox7.Text.Trim();

                            DataTable dt = new DataTable();
                            dt = p.Power.DefaultView.ToTable();
                            dt.Columns.Add();
                            dt.Columns.Add();
                            if (dt.Rows.Count == 0)
                            {
                                dt.Rows.Add("null", "null");
                            }

                            dt.Rows[0][2] = p.RoleID;
                            dt.Rows[0][3] = p.RoleName;
                            DBUser.UpData_PowerByUser(dt);

                            AllRole[p.RoleID] = p;

                            p.Power.AcceptChanges();
                            HasChanged = false;
                            this.comboBox2.Items.Clear();
                            this.comboBox2.Items.AddRange(AllRoleID.ToArray());
                            this.Cursor = Cursors.Default;
                        }
                        else if (dr == DialogResult.Cancel)
                        {
                            e.Cancel = true;
                            return;
                        }
                        else
                        {
                            HasChanged = false;

                            p = AllRole[this.treeView1.SelectedNode.Name];
                            p.Power.RejectChanges();

                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        e.Cancel = true;
                        this.Cursor = Cursors.Default;
                        return;
                    }

                    #endregion
                }
                else if (treeView1.SelectedNode.Parent.Name == "User")
                {
                    #region 保存一个用户
                    if (CheckUser())
                    {
                        try
                        {
                            DialogResult dr = MessageBox.Show("用户保存成功。", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                            if (dr == DialogResult.Yes)
                            {
                                this.Cursor = Cursors.WaitCursor;
                                User OneUser = new User();
                                OneUser.SetUserAttribute(this.txtUserID.Text, this.textBox2.Text, this.textBox2.Text, this.txtPassword.Text, this.comboBox2.Text);

                                DataTable dt = new DataTable();

                                dt = ds.Tables["S_User"];

                                DataRow dr_temp = dt.Rows[0];
                                dt.Clear();
                                dr_temp[0] = OneUser.UserID;

                                dr_temp[1] = OneUser.FamilyName;
                                dr_temp[2] = OneUser.Name;
                                dr_temp[3] = OneUser.Password;
                                dr_temp[4] = OneUser.RoleID;

                                dt.Rows.Add(dr_temp);

                                DBUser.UpData_User(dt);

                                AllUser[OneUser.UserID] = (User)OneUser;
                                HasChanged = false;
                                this.Cursor = Cursors.Default;
                            }
                            else if (dr == DialogResult.Cancel)
                            {
                                e.Cancel = true;
                                return;
                            }
                            else
                            {
                                HasChanged = false;
                                return;
                            }
                        }
                        catch (Exception ex)
                        {
                            e.Cancel = true;
                            this.Cursor = Cursors.Default;
                            return;
                        }

                    }
                    else
                        e.Cancel = true;
                    #endregion
                }

            }
            else
            {
                if (!HasChanged)
                    return;
                if (this.panel1.Visible)
                {
                    #region 保存一个角色
                    try
                    {
                        CanDo = false;
                        treeView1.BeforeSelect -= treeViewEventHandler;
                        treeView1.AfterSelect -= treeViewEventAfterHandler;
                        string selectnodename = e.Node.Name;
                        string selectnodeparentname = String.Empty;
                        if (e.Node.Parent != null)
                            selectnodeparentname = e.Node.Parent.Name;

                        if (this.textBox6.Text.Trim() == String.Empty)
                        {
                            this.textBox6.BackColor = Color.Red;
                            e.Cancel = true;
                            CanDo = true;
                            treeView1.BeforeSelect -= treeViewEventHandler;
                            treeView1.AfterSelect -= treeViewEventAfterHandler;
                            MessageBox.Show("角色ID不要为空。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            treeView1.AfterSelect += treeViewEventAfterHandler;
                            treeView1.BeforeSelect += treeViewEventHandler;

                            this.treeView1.SelectedNode = null;
                            return;
                        }
                        if (AllRole.ContainsKey(this.textBox6.Text.Trim()))
                        {
                            //ShowMessageBox("070111");
                            this.textBox6.BackColor = Color.Red;

                            e.Cancel = true;
                            CanDo = true;
                            treeView1.BeforeSelect -= treeViewEventHandler;
                            treeView1.AfterSelect -= treeViewEventAfterHandler;
                            MessageBox.Show("角色ID重复。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            treeView1.AfterSelect += treeViewEventAfterHandler;
                            treeView1.BeforeSelect += treeViewEventHandler;
                            this.treeView1.SelectedNode = null;
                            return;
                        }
                        DialogResult dr = MessageBox.Show("保存角色?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                        treeView1.AfterSelect += treeViewEventAfterHandler;
                        treeView1.BeforeSelect += treeViewEventHandler;
                        UsePurview p = null;
                        if (dr == DialogResult.Yes)
                        {

                            this.Cursor = Cursors.WaitCursor;

                            p = Purview;

                            p.SetUserAttribute(this.textBox6.Text.Trim(), this.textBox7.Text.Trim());

                            DataTable dt = new DataTable("power");
                            dt = p.Power.DefaultView.ToTable();
                            dt.Columns.Add();
                            dt.Columns.Add();
                            if (dt.Rows.Count == 0)
                            {
                                dt.Rows.Add("null", "null");
                            }
                            dt.Rows[0][2] = p.RoleID;
                            dt.Rows[0][3] = p.RoleName;
                            DBUser.UpData_PowerByUser(dt);
                            //保存新添加的Role
                            //DataProvider.DataProviderFactory.Instance.SystemUserRole.InsertOneRole(p, this.dbRecordTrack);

                            HasChanged = false;
                            this.Cursor = Cursors.Default;
                            e.Cancel = true;

                            AllRole.Add(p.RoleID, p);

                            p.Power.AcceptChanges();

                            AllRoleID.Add(p.RoleID);
                            AllRoleID.Sort();
                            CloneAllRole();
                            AddValueToTreeView();

                            CanDo = true;
                            this.comboBox2.Items.Clear();
                            this.comboBox2.Items.AddRange(AllRoleID.ToArray());
                            int SearchNodeIndex = 0;

                            if (selectnodeparentname == "User")
                            {
                                SearchNodeIndex = 1;
                            }
                            if (selectnodeparentname == String.Empty)
                            {
                                foreach (TreeNode fd in treeView1.Nodes)
                                {
                                    if (fd.Name == selectnodename)
                                        this.treeView1.SelectedNode = fd;
                                }
                            }
                            else
                            {
                                foreach (TreeNode td in this.treeView1.Nodes[SearchNodeIndex].Nodes)
                                {
                                    if (td.Name == selectnodename)
                                        this.treeView1.SelectedNode = td;
                                }
                            }

                        }
                        else if (dr == DialogResult.Cancel)
                        {

                            e.Cancel = true;
                            this.treeView1.SelectedNode = null;
                            CanDo = true;
                            return;
                        }
                        else
                        {
                            HasChanged = false;
                            CanDo = true;
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        e.Cancel = true;
                        this.treeView1.SelectedNode = null;
                        CanDo = true;
                        this.Cursor = Cursors.Default;
                        return;
                    }
                    #endregion
                }
                else
                {
                    #region 保存一个用户

                    try
                    {
                        CanDo = false;
                        string selectnodename = e.Node.Name;
                        string selectnodeparentname = String.Empty;
                        if (e.Node.Parent != null)
                            selectnodeparentname = e.Node.Parent.Name;

                        treeView1.BeforeSelect -= treeViewEventHandler;
                        treeView1.AfterSelect -= treeViewEventAfterHandler;
                        DialogResult dr = MessageBox.Show("保存用户?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                        treeView1.BeforeSelect += treeViewEventHandler;
                        treeView1.AfterSelect += treeViewEventAfterHandler;

                        if (dr == DialogResult.Yes)
                        {
                            if (CheckUser())
                            {
                                this.Cursor = Cursors.WaitCursor;
                                User OneUser = new User();
                                OneUser.SetUserAttribute(this.txtUserID.Text.Trim(), this.textBox2.Text.Trim(), this.textBox2.Text.Trim(), this.txtPassword.Text, this.comboBox2.Text);
                                OneUser.MenuID = this.comboBox1.Text;

                                DataTable dt = new DataTable();

                                dt = ds.Tables["S_User"];

                                DataRow dr_temp = dt.Rows[0];
                                dt.Clear();
                                dr_temp[0] = OneUser.UserID;

                                dr_temp[1] = OneUser.FamilyName;
                                dr_temp[2] = OneUser.Name;
                                dr_temp[3] = OneUser.Password;
                                dr_temp[4] = OneUser.RoleID;

                                dt.Rows.Add(dr_temp);

                                DBUser.Insert_User(dt);
                                AllUser.Add(OneUser.UserID, OneUser);
                                AllUserID.Add(OneUser.UserID);
                                AllUserID.Sort();
                                CloneAllUser();
                                HasChanged = false;

                                e.Cancel = true;
                                CanDo = true;
                                AddValueToTreeView();

                                this.Cursor = Cursors.Default;
                                int SearchNodeIndex = 0;

                                if (selectnodeparentname == "User")
                                {
                                    SearchNodeIndex = 1;
                                }
                                if (selectnodeparentname == String.Empty)
                                {
                                    foreach (TreeNode fd in treeView1.Nodes)
                                    {
                                        if (fd.Name == selectnodename)
                                            this.treeView1.SelectedNode = fd;
                                    }
                                }
                                else
                                {
                                    foreach (TreeNode td in this.treeView1.Nodes[SearchNodeIndex].Nodes)
                                    {
                                        if (td.Name == selectnodename)
                                            this.treeView1.SelectedNode = td;
                                    }
                                }

                            }
                            else
                            {
                                e.Cancel = true;
                                this.treeView1.SelectedNode = null;
                                CanDo = true;

                            }

                        }
                        else if (dr == DialogResult.Cancel)
                        {
                            e.Cancel = true;
                            this.treeView1.SelectedNode = null;
                            CanDo = true;
                            return;
                        }
                        else
                        {
                            HasChanged = false;
                            CanDo = true;
                            return;
                        }
                    }

                    catch (Exception ex)
                    {
                        e.Cancel = true;
                        this.treeView1.SelectedNode = null;
                        CanDo = true;
                        this.Cursor = Cursors.Default;
                        return;
                    }
                    finally
                    {
                        this.Cursor = Cursors.Default;
                    }
                } return;
                    #endregion

            }
        }
        private void LoadAllUser()
        {
            AllUserID = new List<string>();
            AllUser = new Dictionary<string, User>();
            DataTable dt = DBUser.GetAllUserID();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                AllUserID.Add(dt.Rows[i][0].ToString().Trim());
            }

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                User temp_user = new User();
                temp_user.SetUserAttribute(dt.Rows[i][0].ToString().Trim(), dt.Rows[i][1].ToString().Trim(), dt.Rows[i][2].ToString().Trim(), dt.Rows[i][3].ToString().Trim(), dt.Rows[i][4].ToString().Trim());

                AllUser.Add(dt.Rows[i][0].ToString().Trim(), temp_user);
            }

            TempAllUser = new Dictionary<string, User>();
            foreach (string s in AllUser.Keys)
            {
                User Temp = (User)AllUser[s].GetUser();
                Temp.SetUserAttribute(s, AllUser[s].FamilyName, AllUser[s].Name, AllUser[s].Password, AllUser[s].RoleID);
                TempAllUser.Add(s, Temp);
            }
            AllUserID.Sort();
        }