C# 以编程方式循环遍历 DatagridView 并选中复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/681046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:12:26  来源:igfitidea点击:

Programatically loop through a DatagridView and check checkboxes

c#windowsdatagridviewcheckboxcell

提问by

I have DataGridView bound by a datatable i have checkboxes to the same.

我有一个数据表绑定的 DataGridView 我有相同的复选框。

I want to navigate or loop through the the datagridview and check mark these checkboxes ,Below is the syntax i use .

我想在 datagridview 中导航或循环并选中这些复选框,下面是我使用的语法。

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.!
}

回答by Marc Gravell

If it is bound to a DataTable, can you not work on the model (the table) instead? The DataGridViewis a view...

如果它绑定到 a DataTable,你不能在模型(表)上工作吗?这DataGridView是一个视图...

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:

尝试遍历表中的行,设置值。例如(下面) - 请注意,我不更新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);
    }
}

回答by Justin Bannister

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;
}

回答by Justin Bannister

The following worked for me, it checked the checkboxes perfectly :)

以下对我有用,它完美地检查了复选框:)

foreach (DataGridViewRow row in dgvDataGridView.Rows)
{
    ((DataGridViewCheckBoxCell)row.Cells[0]).Value = true;
}

回答by Arv

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.

选择其值的行不会传递到基础数据源,因此不会保存。数据源是Datatable。它的datagridview的问题。

回答by Duncan Carr

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.

这看起来像你需要的那样。我对这一切都很陌生,如果我回答不正确,我很抱歉。只是想帮忙。