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
- C# Winform的DataGridView中的chckbox如何全选,单选,多选
- C# Winform DataGridView 全选
- C# winform DataGridView 行全选反选
- [WINFORM]C#中的DataGridView
- C# Winform中DataGridView 设置全选/取消
- chckbox 实现单选效果(html)
- winform中DataGridView全选和反选
- C# winform关于datagridview中的列的数据类型转换问题
- C# WinForm下DataGridView单选按钮列和支持三种选择状态的复选框列的实现
- C# WinForm下DataGridView单选按钮列和支持三种选择状态的复选框列的实现
- winform datagridview 表头加全选
- .net C# listbox的全选,反选和全不选,WinForm
- Winform中DataGridView实现全选和反选
- c# winform DataGridView单击选中一整行,只能单选,不能选择多行,只能选择一行
- c# winform 关于DataGridView的一些操作
- c# winform 关于DataGridView的一些操作
- C# Winform中DataGridView的DataGridViewCheckBoxColumn使用方法
- c# winform 关于DataGridView的一些操作
- 安卓图片模糊化
- 中点Bresenham算法扫描转换圆心在原点, 半径为8的圆;
- BigWorld Server
- PlacePicker调用导致GooglePlayservier崩溃
- eclipse 头疼的 jsp format
- C# Winform的DataGridView中的chckbox如何全选,单选,多选
- Android 1000实例代码集结(一 )
- Linux运维笔记-文档总结-FTP介绍,安装,以及匿名用户和本地用户的操作
- 关于ThinkPHP的JPush推送(亲测可用)
- 经典SQL语句大全
- Qt 漂亮的表单之QSS使用
- JQuery实现导航隐藏关闭和鼠标事件
- “Cannot read property 'classList' of null” when using classList.add
- ssh-key