/*
'' ============================================================================================
'' 项目名称：风越项目范例WinForm单层框架_dll
'' 项目版本：V1.0
'' 项目描述：
'' 文件名称：YongHuXinXi_Search.cs
'' 文件描述：信息搜索文件 （用户信息）
'' 公司名称：中极网盛
'' 开发人员：刘怒
'' 创建日期：2008-12-24 21:26:37
'' 修订日期：2009-03-10 11:56:07
'' 版权信息：Copyright (C) 2008

'' 项目升级时需读取以下生成工具的版权信息
'' 生成工具：风越代码生成器 [FireCode Creator]
'' 当前版本：通用版 V1.8
'' 官方网站：http://www.sino8848.com
'' ============================================================================================
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using FypSystem.Common;
using FypSystem.DBManager;

namespace fyp_winform_dll
{
    public partial class YongHuXinXi_Search : Form
    {
        public YongHuXinXi_Search()
        {
            InitializeComponent();

            Initialize();
            FillGrid();
        }

        /// <summary>
        /// 设置默认值、数据
        /// </summary>
        private void Initialize()
        {
            this.Text += " - " + FypSystem.Common.WebPage.GetConfig("fypAppName");
            
            
            this.cbx_SelectAll.Visible = false;
            
        }

        /// <summary>
        /// 填充数据网格
        /// </summary>
        public void FillGrid()
        {
            this.dataGridView1.AutoGenerateColumns = false;

            
            using (FypSystem.DBManager.Access dBManager = new FypSystem.DBManager.Access())
            
            
            {
                //string strSql = "select [USER_ID], [USER_NAME], [USER_PWD], [USER_BIRTHDAY], [USER_PHOTO], [USER_COMPANY], [USER_DEPT], [USER_INFO] from [USER]";
                //string strSql = "select [USER_ID], [USER_NAME], [USER_PWD], [USER_BIRTHDAY], [USER_PHOTO], (select [COMPANY_NAME] from [COMPANY] where [COMPANY_ID]=[USER_COMPANY]) as [COMPANY_NAME], (select [DEPARTMENT_NAME] from [DEPARTMENT] where [DEPARTMENT_ID]=[USER_DEPT]) as [DEPARTMENT_NAME], [USER_INFO] from [USER]";

                string strSql = "select [USER_ID], [USER_NAME], [USER_PWD], [USER_BIRTHDAY], [USER_PHOTO], [COMPANY_NAME], [DEPARTMENT_NAME], [USER_INFO] from (([USER] LEFT JOIN [COMPANY] ON [COMPANY].[COMPANY_ID]=[USER].[USER_COMPANY]) LEFT JOIN [DEPARTMENT] ON [DEPARTMENT].[DEPARTMENT_ID]=[USER].[USER_DEPT])";
                DataTable tabSrouce = dBManager.GetDataTable(strSql);

                this.dataGridView1.DataSource = tabSrouce;
                tabSrouce.Dispose();
            }
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            FillGrid();
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            YongHuXinXi_Add Form_Add = new YongHuXinXi_Add();
            Form_Add.ShowDialog(this);
        }

        private void btn_Edit_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentCell != null && this.dataGridView1.CurrentCell.RowIndex > -1)
            {
                int iRowIdx = this.dataGridView1.CurrentCell.RowIndex;
            
                string strUSER_ID = this.dataGridView1.Rows[iRowIdx].Cells["col_USER_ID_PrimaryKey"].Value.ToString();
            
                string strUSER_NAME = this.dataGridView1.Rows[iRowIdx].Cells["col_USER_NAME_PrimaryKey"].Value.ToString();
            
                YongHuXinXi_Add Form_Add = new YongHuXinXi_Add(strUSER_ID, strUSER_NAME);
                Form_Add.ShowDialog(this);
            }
        }

        private void btn_Del_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否删除所选择记录？", "提示：", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            
            using (FypSystem.DBManager.Access dBManager = new Access())
            
            
            {
                
                string strAllID_USER_ID = "";
                
                string strAllID_USER_NAME = "";
                
                foreach (DataGridViewRow row in this.dataGridView1.Rows)
                {
                    if (row.Index >= 0 && row.Cells["col_Checked"].Value != null && row.Cells["col_Checked"].Value.ToString() == "True")
                    {
                        
                        strAllID_USER_ID += "" + row.Cells["col_USER_ID_PrimaryKey"].Value.ToString() + ",";
                        
                        strAllID_USER_NAME += "'" + row.Cells["col_USER_NAME_PrimaryKey"].Value.ToString() + "',";
                        
                    }
                }
                if (strAllID_USER_ID.Length > 0 && strAllID_USER_NAME.Length > 0)
                {
                    
                    strAllID_USER_ID = FypSystem.Common.StringExtend.TrimRight(strAllID_USER_ID, ",");
                    
                    strAllID_USER_NAME = FypSystem.Common.StringExtend.TrimRight(strAllID_USER_NAME, ",");
                    
                    string strSql;
                    string strWhere = " where [USER_ID] in (" + strAllID_USER_ID + ")  and [USER_NAME] in (" + strAllID_USER_NAME + ") ";
                    strSql = "delete from [USER] " + strWhere;
                    //MessageBox.Show(strSql);
                    dBManager.ExecNonQuery(strSql);
                }
                FillGrid();
            }
        }

        private void cbx_SelectAll_CheckedChanged(object sender, EventArgs e)
        {
            bool bChecked = this.cbx_SelectAll.Checked;
            foreach (DataGridViewRow row in this.dataGridView1.Rows)
            {
                if (row.Index >= 0)
                {
                    row.Cells["col_Checked"].Value = bChecked;
                }
            }
        }
    }
}