C# Winform的DataGridView中的chckbox如何全选,单选,多选
2017-04-27 17:19
435 查看
点击表头进行全选,在DataGridView中的单击事件写一下代码,
多选,在DataGridView的点击单元格内容的单击事件写一下代码,如果只要求单选而不多选的话,则将注释掉的那几行代码去掉注释
将选中的一行或多行数据从DataGridView放到datatable中,然后进行打印
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)), aee0 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); } }
相关文章推荐
- C#给WinForm(窗体)下DataGridView控件添加单选选或多选按钮列
- C# WinForm下DataGridView单选按钮列和支持三种选择状态的复选框列的实现
- C#winform datagridview如何自定义列头
- C# winform datagridview中如何实现鼠标右键点击一行数据出现一个带有删除的菜单,并能执行删除操作?
- c# winform DataGridView单击选中一整行,只能单选,不能选择多行,只能选择一行
- Winform DataGridView CheckBoxColumn c# 单选 解决方案
- C# WinForm下DataGridView单选按钮列和支持三种选择状态的复选框列的实现
- C# Winform DataGridView 中如何动态插入combox
- C# Winform DataGridView 全选
- C#Winform中如何给DataGridView添加一个自增列
- c# winform DataGridView单击选中一整行,只能单选,不能选择多行,只能选择一行
- c# winform DataGridView单击选中一整行,只能单选,不能选择多行,只能选择一行
- C# winform DataGridView 行全选反选
- 简单在线调查,单选和多选(c#)
- 【原创】浅谈如何在C#Winform程序中正确使用登录窗体
- 发布C#(Winform)datagridview 扩展控件
- C# Winform程序如何获取运行路径
- 在C#中如何实现Form与Form之间的通信(winform 内容)
- 一段源码,展示C# winform 如何识别 Ctrl+Shift+X组合键;如何选取离光标最近的一个单词。
- C# winform 程序中如何限制一个文本框中只输入数字和字母(撤消键可以用)