winform:datagridview筛选与选择

winform:datagridview筛选与选择_第1张图片

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

namespace ESignature
{
    public partial class Form1 : CCSkinMain
    {
        static  string[] m_arrPorject = new string[] { "项目1", "项目2", "项目3" };
        private List m_listProject = new List(m_arrPorject);
        List m_listWait = new List();
        Dictionary m_dicRowId = new Dictionary();

        List m_listHaveSign = new List();
        List m_listFailSign = new List();
        public Form1()
        {
            InitializeComponent();

            DataInit();
            PanelInit();

            
        }

        public void DataInit()
        {
            m_listProject.Insert(0, "选择所属项目");
            InsertPorjectCom(skinComboBoxWait, m_listProject);
            InsertPorjectCom(CbHaveSign, m_listProject);
            InsertPorjectCom(CbFailSign, m_listProject);
            //InsertPorjectCom(skinComboBox1, m_listProject);

            SignItemDataInit(m_listWait);

            SignItemDataInit(m_listHaveSign);
            SignItemDataInit(m_listFailSign);

        }

        public void PanelInit()
        {
            WaitGridUpdate("");
            HaveSignUpdate("");
            FailSignUpdate("");
            //WaitAddCheck();
        }

        void SignItemDataInit(List list)
        {
            SignItem it0 = new SignItem();
            it0.id = 0;
            it0.projectName = "项目1";
            it0.approvalName = "电器";
            it0.creator = "小张";
            it0.date = DateTime.Now.ToString();
            it0.state = 1;
            list.Add(it0);

            SignItem it1 = new SignItem();
            it1.id = 1;
            it1.projectName = "项目2";
            it1.approvalName = "电器";
            it1.creator = "小张";
            it1.date = DateTime.Now.ToString();
            it1.state = 0;
            list.Add(it1);

            SignItem it2 = new SignItem();
            it2.id = 2;
            it2.projectName = "项目3";
            it2.approvalName = "电器";
            it2.creator = "小张";
            it2.date = DateTime.Now.ToString();
            it2.state = 1;
            list.Add(it2);
        }
        public void InsertPorjectCom(ComboBox cb, List list)
        {
            
            cb.DataSource = list.ToList();

            

        }

        /// 
        /// 批量签章
        /// 
        /// 
        /// 
        private void skinButton1_Click(object sender, EventArgs e)
        {
            string selectRows = "";
            for (int i = 0; i < GridWait.Rows.Count ; i++) //循环datagridview每行
            {
                if ((bool)GridWait.Rows[i].Cells[0].EditedFormattedValue == true)
                {
                    selectRows = selectRows + "[" + m_dicRowId[i] + "]";

                }
            }

            MessageBox.Show("Selected id:" + selectRows, "CheckBoxRows");
        }

        /// 
        /// 刷新批量签章
        /// 
        /// 
        /// 
        private void BtnRefeshWait_Click(object sender, EventArgs e)
        {

        }

        void FailSignUpdate(string projectName)
        {
            m_dicRowId.Clear();
            int cnt = 0;
            for (int i = GridFailSign.RowCount - 1; i >= 0; i--)
            {
                GridFailSign.Rows.Remove(GridFailSign.Rows[i]);
            }

            foreach (var u in m_listFailSign)//遍历查询出来的数据
            {
                if (projectName == u.projectName || projectName == "")
                {
                    m_dicRowId[cnt] = u.id;
                    cnt++;

                    DataGridViewRow row = new DataGridViewRow();

                    DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
                    textboxcell.Value = u.projectName;
                    row.Cells.Add(textboxcell);

                    DataGridViewTextBoxCell textboxcel2 = new DataGridViewTextBoxCell();
                    textboxcel2.Value = u.approvalName;
                    row.Cells.Add(textboxcel2);

                    DataGridViewTextBoxCell textboxcel4 = new DataGridViewTextBoxCell();
                    textboxcel4.Value = u.creator;
                    row.Cells.Add(textboxcel4);

                    DataGridViewTextBoxCell textboxcel5 = new DataGridViewTextBoxCell();
                    textboxcel5.Value = u.date;
                    row.Cells.Add(textboxcel5);

                    DataGridViewButtonCell textboxcel6 = new DataGridViewButtonCell();
                    textboxcel6.Value = "重新签章";
                    row.Cells.Add(textboxcel6);

                    DataGridViewButtonCell textboxcel7 = new DataGridViewButtonCell();
                    textboxcel7.Value = "查看文件";
                    row.Cells.Add(textboxcel7);

                    GridFailSign.Rows.Add(row);
                }
            }
        }

        void HaveSignUpdate(string projectName)
        {
            m_dicRowId.Clear();
            int cnt = 0;
            for (int i = GradHaveSign.RowCount - 1; i >= 0; i--)
            {
                GradHaveSign.Rows.Remove(GradHaveSign.Rows[i]);
            }

            foreach (var u in m_listHaveSign)//遍历查询出来的数据
            {
                if (projectName == u.projectName || projectName == "")
                {
                    m_dicRowId[cnt] = u.id;
                    cnt++;

                    DataGridViewRow row = new DataGridViewRow();

                    DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
                    textboxcell.Value = u.projectName;
                    row.Cells.Add(textboxcell);

                    DataGridViewTextBoxCell textboxcel2 = new DataGridViewTextBoxCell();
                    textboxcel2.Value = u.approvalName;
                    row.Cells.Add(textboxcel2);

                    DataGridViewTextBoxCell textboxcel4 = new DataGridViewTextBoxCell();
                    textboxcel4.Value = u.creator;
                    row.Cells.Add(textboxcel4);

                    DataGridViewTextBoxCell textboxcel5 = new DataGridViewTextBoxCell();
                    textboxcel5.Value = u.date;
                    row.Cells.Add(textboxcel5);

                    DataGridViewButtonCell textboxcel6 = new DataGridViewButtonCell();
                    textboxcel6.Value = "详情";
                    row.Cells.Add(textboxcel6);

                    GradHaveSign.Rows.Add(row);
                }
            }
        }

        private void WaitGridUpdate(string projectName)
        {
            m_dicRowId.Clear();
            int cnt = 0;
            for (int i = GridWait.RowCount-1; i >= 0 ; i--)
            {
                GridWait.Rows.Remove(GridWait.Rows[i]);
            }

            foreach (var u in m_listWait)//遍历查询出来的数据
            {
                if (projectName == u.projectName || projectName == "")
                {
                    m_dicRowId[cnt] = u.id;
                    cnt++;

                    DataGridViewRow row = new DataGridViewRow();

                    DataGridViewCheckBoxCell comboxcell = new DataGridViewCheckBoxCell();
                    row.Cells.Add(comboxcell);


                    DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
                    textboxcell.Value = u.projectName;
                    row.Cells.Add(textboxcell);

                    DataGridViewTextBoxCell textboxcel2 = new DataGridViewTextBoxCell();
                    textboxcel2.Value = u.approvalName;
                    row.Cells.Add(textboxcel2);

                    DataGridViewTextBoxCell textboxcel3 = new DataGridViewTextBoxCell();
                    switch (u.state)
                    {
                        case 0:
                            textboxcel3.Value = "进行中";
                            break;
                        case 1:
                            textboxcel3.Value = "已完成";

                            break;
                        default:
                            break;
                    }

                    row.Cells.Add(textboxcel3);

                    DataGridViewTextBoxCell textboxcel4 = new DataGridViewTextBoxCell();
                    textboxcel4.Value = u.creator;
                    row.Cells.Add(textboxcel4);

                    DataGridViewTextBoxCell textboxcel5 = new DataGridViewTextBoxCell();
                    textboxcel5.Value = u.date;
                    row.Cells.Add(textboxcel5);

                    DataGridViewButtonCell textboxcel6 = new DataGridViewButtonCell();
                    textboxcel6.Value = "详情";
                    row.Cells.Add(textboxcel6);

                    if (u.state == 1)
                    {
                        DataGridViewButtonCell textboxcel7 = new DataGridViewButtonCell();
                        textboxcel7.Value = "一键签章";
                        row.Cells.Add(textboxcel7);
                    }
                    GridWait.Rows.Add(row);
                }
            }

          

            
        }

        private void skinDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 )
            {
                DataGridViewColumn column = GridWait.Columns[e.ColumnIndex];
                switch (e.ColumnIndex)
                {
                    case 6:
                        //MessageBox.Show("查看 id" + m_dicWait[e.RowIndex] + "详情");
                        //System.Diagnostics.Process.Start("http://www.baidu.com");

                        //连接
                        string url = "http://www.baidu.com";
                        //定义脚本
                        string script = @"";
                        WebBrowser wb = new WebBrowser();
                        wb.DocumentText = @" " + script + "";//定义WebBrowser中的DOM文档
                        wb.DocumentCompleted += delegate
                        {
                            //执行脚本函数
                            wb.Document.InvokeScript("openUrl", new object[] { url });
                        };

                        break;
                    case 7:
                        MessageBox.Show("一键签章" + m_dicRowId[e.RowIndex]);
                        break;
                    default:
                        break;
                }
               

            }
        }

        void WaitAddCheck()
        {
            DataGridViewCheckBoxColumn columncb = new DataGridViewCheckBoxColumn();
            columncb.HeaderText = "选择";
            columncb.Name = "cb_check";
            columncb.TrueValue = true;
            columncb.FalseValue = false;
            columncb.DataPropertyName = "IsChecked";
            GridWait.Columns.Insert(0, columncb);    //添加的checkbox在第一列
        }

        private void GridWait_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {

        }

        private void GridWait_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            if (GridWait.Rows[e.RowIndex].IsNewRow) return;
        }

        private void skinComboBoxWait_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (skinComboBoxWait.Text)
            {
                case "选择所属项目":
                    WaitGridUpdate("");
                    break;
                default:
                    WaitGridUpdate(skinComboBoxWait.Text);
                    break;
            }
        }

        private void CbHaveSign_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (CbHaveSign.Text)
            {
                case "选择所属项目":
                    HaveSignUpdate("");
                    break;
                default:
                    HaveSignUpdate(CbHaveSign.Text);
                    break;
            }
        }

        private void GradHaveSign_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void CbFailSign_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (CbFailSign.Text)
            {
                case "选择所属项目":
                    HaveSignUpdate("");
                    break;
                default:
                    HaveSignUpdate(CbFailSign.Text);
                    break;
            }
        }
    }


    public class SignItem
    {
        public int id; 
        public string projectName;
        public string approvalName;
        public int state;
        public string creator;
        public string date;
    }
}

你可能感兴趣的:(winform)