问题
I have DataGridView bound by a datatable i have checkboxes to the same.
I want to navigate or loop through the the datagridview and check mark these checkboxes ,Below is the syntax i use .
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.!
}
回答1:
The following worked for me, it checked the checkboxes perfectly :)
foreach (DataGridViewRow row in dgvDataGridView.Rows)
{
((DataGridViewCheckBoxCell)row.Cells[0]).Value = true;
}
回答2:
If it is bound to a DataTable
, can you not work on the model (the table) instead? The DataGridView
is a view...
Try looping over the rows in the table, setting the values. For example (below) - note that I don't update the DataGridView
- just the 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);
}
}
回答3:
Something along the lines of:
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;
}
回答4:
the row that is selected its value do not get passed to the underlying datasource so do not get saved. the datasource is Datatable. Its problemof datagridview.
回答5:
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; }
}
}
This looks like it does what you require. I'm new to all this so sorry if I have answered incorrectly. Just trying to help.
来源:https://stackoverflow.com/questions/681046/programatically-loop-through-a-datagridview-and-check-checkboxes