c# – 检查DataGridView上的所有Checkbox项目
发布时间:2020-12-16 00:21:39 所属栏目:百科 来源:网络整理
导读:这是场景. 我有复选框(名称:“全部检查”ID:chkItems)和datagridview.当我点击这个复选框时,也会检查datagridview上的所有复选框. 我还在网格上添加了复选框列. DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();CheckBox c
这是场景.
我有复选框(名称:“全部检查”ID:chkItems)和datagridview.当我点击这个复选框时,也会检查datagridview上的所有复选框. 我还在网格上添加了复选框列. DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn(); CheckBox chk = new CheckBox(); CheckboxColumn.Width = 20; GridView1.Columns.Add(CheckboxColumn); 这是复选框背后的代码. row.Cell上有一个问题 private void chkItems_CheckedChanged(object sender,EventArgs e) { foreach (DataGridViewRow row in GridView1.Rows) { DataGridViewCheckBoxCell chk = e.row.Cells(0); if (chk.Selected == false) { row.Cells(0).Value = true; } } } 已解决(这是解决方案) private void chkItems_CheckedChanged(object sender,EventArgs e) { foreach (DataGridViewRow row in GridView1.Rows) { DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[1]; if (chk.Selected == false) { chk.Selected = true; } } } 解决方法DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell) row.Cells[0]; 代替 DataGridViewCheckBoxCell chk = e.row.Cell(0); *编辑:*我认为你真的想这样做: foreach (DataGridViewRow row in dataGridView1.Rows) { DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell) row.Cells[0]; chk.Value = !(chk.Value == null ? false : (bool) chk.Value); //because chk.Value is initialy null } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |