C# 以编程方式在DatagridView中循环并选中复选框
我将DataGridView绑定到一个datatable,并将复选框绑定到该datatable 我想在datagridview中导航或循环,并选中这些复选框,下面是我使用的语法C# 以编程方式在DatagridView中循环并选中复选框,c#,windows,datagridviewcheckboxcell,C#,Windows,Datagridviewcheckboxcell,我将DataGridView绑定到一个datatable,并将复选框绑定到该datatable 我想在datagridview中导航或循环,并选中这些复选框,下面是我使用的语法 foreach(DataGridViewRow dr in dgvColumns.Rows) { DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dr.Cells["CheckBoxes"]; checkCell
foreach(DataGridViewRow dr in dgvColumns.Rows)
{
DataGridViewCheckBoxCell checkCell =
(DataGridViewCheckBoxCell)dr.Cells["CheckBoxes"];
checkCell.Value=1;
//Also tried checkCell.Selected=true;
//Nothing seems to have worked.!
}
如果它绑定到一个
数据表
,您不能改为处理模型(表)吗?DataGridView
是一个视图
尝试在表中的行上循环,设置值。例如(如下)-请注意,我不更新DataGridView
-只更新DataTable
:
using System;
using System.Data;
using System.Windows.Forms;
static class Program
{
[STAThread]
static void Main()
{
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Selected", typeof(bool));
table.Rows.Add("Fred", false);
table.Rows.Add("Jo", false);
table.Rows.Add("Andy", true);
Button btn = new Button();
btn.Text = "Select all";
btn.Dock = DockStyle.Bottom;
btn.Click += delegate
{
foreach (DataRow row in table.Rows)
{
row["Selected"] = true;
}
};
DataGridView grid = new DataGridView();
grid.Dock = DockStyle.Fill;
grid.DataSource = table;
Form form = new Form();
form.Controls.Add(grid);
form.Controls.Add(btn);
Application.Run(form);
}
}
大致如下:
foreach(DataGridViewRow dgvr in dgvColumns.Rows)
{
// Get the underlying datarow
DataRow dr = ((DataRowView)dgvr.DataBoundItem).Row;
// Update the appropriate column in the data row.
// Assuming this is your column name in your
// underlying data table
dr["CheckBoxes"] = 1;
}
以下内容对我有效,它完美地勾选了复选框:)
选择其值的行不会传递到基础数据源,因此不会保存。数据源是Datatable。它的问题是datagridview。
使用System.Collections.Generic;
using System.Collections.Generic;
using System.Windows.Forms;
namespace FindTheCheckedBoxes
{
public partial class Form1 : Form
{
List<TestObject> list = new List<TestObject>();
List<int> positionId = new List<int>();
public Form1()
{
InitializeComponent();
PopulateDataGrid();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if ((bool)row.Cells[0].Value == true)
positionId.Add((int)row.Cells[1].Value);
}
// sets the window title to the columns found ...
this.Text = string.Join(", ", positionId);
}
void PopulateDataGrid()
{
list.Add(new TestObject { tick = false, LineNum = 1 });
list.Add(new TestObject { tick = true, LineNum = 2 });
list.Add(new TestObject { tick = false, LineNum = 3 });
list.Add(new TestObject { tick = true, LineNum = 4 });
dataGridView1.DataSource = list;
}
}
class TestObject
{
public bool tick { get; set; }
public int LineNum { get; set; }
}
}
使用System.Windows.Forms;
命名空间找到了复选框
{
公共部分类Form1:Form
{
列表=新列表();
列表位置ID=新列表();
公共表格1()
{
初始化组件();
填充数据网格();
foreach(dataGridView1.Rows中的DataGridViewRow行)
{
if((bool)行。单元格[0]。值==true)
positionId.Add((int)row.Cells[1].Value);
}
//将窗口标题设置为找到的列。。。
this.Text=string.Join(“,”,positionId);
}
void PopulateDataGrid()
{
Add(新的TestObject{tick=false,LineNum=1});
Add(新的TestObject{tick=true,LineNum=2});
Add(新的TestObject{tick=false,LineNum=3});
Add(新的TestObject{tick=true,LineNum=4});
dataGridView1.DataSource=列表;
}
}
类TestObject
{
公共bool tick{get;set;}
public int LineNum{get;set;}
}
}
这看起来像是你所需要的。我对这一切都不熟悉,如果我回答不正确,我很抱歉。只是想提供帮助。复选框是否与数据表中的任何字段相关?这可能是一个模板字段吗?。如果您发布标记,可能会很清楚复选框与任何toher字段无关,但基于某些值,我需要在datagrid((DataGridRowView)dgvr.DataBoundItem)上检查这些值。Row Row Row没有类似于te Row的属性?我在datagridView中也有一个comboBox,是否还要将其设置为某个值?如何做到这一点?没有单独添加,实际上datagridview 1.CheckBox(非数据绑定!)2中有5列。TextBox(绑定)3.ComboBox(非数据绑定)4.ComboBox(非数据绑定)5.TextBox(非数据绑定!)假设您已通过添加项用数据填充组合。只需获取对DataGridComboxCell的引用并设置该项的值。但该项必须在列表中。感谢Marc提供的代码。我是否可以从datagridview中的组合框中选择一个值?如果是数据绑定的,那么更改绑定值也可以解决这个问题,我相信。
using System.Collections.Generic;
using System.Windows.Forms;
namespace FindTheCheckedBoxes
{
public partial class Form1 : Form
{
List<TestObject> list = new List<TestObject>();
List<int> positionId = new List<int>();
public Form1()
{
InitializeComponent();
PopulateDataGrid();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if ((bool)row.Cells[0].Value == true)
positionId.Add((int)row.Cells[1].Value);
}
// sets the window title to the columns found ...
this.Text = string.Join(", ", positionId);
}
void PopulateDataGrid()
{
list.Add(new TestObject { tick = false, LineNum = 1 });
list.Add(new TestObject { tick = true, LineNum = 2 });
list.Add(new TestObject { tick = false, LineNum = 3 });
list.Add(new TestObject { tick = true, LineNum = 4 });
dataGridView1.DataSource = list;
}
}
class TestObject
{
public bool tick { get; set; }
public int LineNum { get; set; }
}
}