C# Winform的DataGridView中的chckbox如何全选,单选,多选

来源:互联网 发布:海尔网络电视价格 编辑:程序博客网 时间:2024/06/06 06:29

点击表头进行全选,在DataGridView中的单击事件写一下代码,

   private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)        {            if (e.RowIndex == -1)//如果单击列表头,全选.            {                int i;                for (i = 0; i < this.dataGridView1.RowCount; i++)                {                    this.dataGridView1.Rows[i].Cells[0].Value = "true";//如果为true则为选中,false未选中                }            }        }

多选,在DataGridView的点击单元格内容的单击事件写一下代码,如果只要求单选而不多选的话,则将注释掉的那几行代码去掉注释

  private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)        {            try            {                int index = dataGridView1.CurrentRow.Index;                this.dataGridView1.Rows[e.RowIndex].Selected = true;                if (Convert.ToBoolean(dataGridView1.Rows[index].Cells[0].Value))                {                    dataGridView1.Rows[index].Cells[0].Value = false;                }                else                {                    dataGridView1.Rows[index].Cells[0].Value = true;                    ////其他的都是false                    //for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)                    //{                    //    if (i != index)                    //    {                    //        dataGridView1.Rows[i].Cells[0].Value = false;                    //    }                    //}                }            }            catch            {            }        }

全选

多选

将选中的一行或多行数据从DataGridView放到datatable中,然后进行打印

  DataGridViewCheckBoxCell checkCell;        private void btnPrint_Click(object sender, EventArgs e)        {            try            {                DataTable dt = new DataTable();                dt.Columns.AddRange(new DataColumn[] {                new DataColumn("id",typeof(Int32)),                new DataColumn("cinvname",typeof(string)),                new DataColumn("cinvstd",typeof(string)),                new DataColumn("supplier",typeof(string)),                new DataColumn("weight",typeof(string)),                new DataColumn("orderNo",typeof(string)),                new DataColumn("producteddate",typeof(string)),                new DataColumn("mocode",typeof(string)),                new DataColumn("supplierbatchno",typeof(string)),                new DataColumn("packno",typeof(string)),                new DataColumn("qty",typeof(string))            });                for (int i = 0; i < dataGridView1.Rows.Count; i++)                {                    checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["Column1"];                    if (Convert.ToBoolean(checkCell.Value) == true)                    {                        DataRow dr = null;                        dr = dt.NewRow();                        //将选中的一行数据转换到datatable中                        dr["id"] = Convert.ToString(dataGridView1.Rows[i].Cells["id"].Value);                        dr["cinvname"] = Convert.ToString(dataGridView1.Rows[i].Cells["cinvname"].Value);                        dr["cinvstd"] = Convert.ToString(dataGridView1.Rows[i].Cells["cinvstd"].Value);                        dr["supplier"] = Convert.ToString(dataGridView1.Rows[i].Cells["supplier"].Value);                        dr["weight"] = Convert.ToString(dataGridView1.Rows[i].Cells["weight"].Value);                        dr["orderNo"] = Convert.ToString(dataGridView1.Rows[i].Cells["orderNo"].Value);                        dr["producteddate"] = Convert.ToString(dataGridView1.Rows[i].Cells["producteddate"].Value);                        dr["mocode"] = Convert.ToString(dataGridView1.Rows[i].Cells["mocode"].Value);                        dr["supplierbatchno"] = Convert.ToString(dataGridView1.Rows[i].Cells["supplierbatchno"].Value);                        dr["packno"] = Convert.ToString(dataGridView1.Rows[i].Cells["packno"].Value);                        dr["qty"] = Convert.ToString(dataGridView1.Rows[i].Cells["qty"].Value);                        dt.Rows.Add(dr);                        LoginUser bll = new LoginUser();                        string id = dr["id"].ToString();                        bll.UptIsExcel(id);                    }                }//for                LoginUser bl = new LoginUser();                DataTable dt2 = bl.selectIsExcel();                this.dataGridView1.DataSource = dt2;                PreviewColorReport(dt, "标签打印模板.frx");            }            catch (Exception ex)            {                MessageBox.Show("异常信息:" + ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);            }        }
1 0