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

发布于 2024-07-15 04:45:54 字数 370 浏览 8 评论 0原文

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

我想导航或循环浏览 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.!
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

送君千里 2024-07-22 04:45:54

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

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

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

foreach (DataGridViewRow row in dgvDataGridView.Rows)
{
    ((DataGridViewCheckBoxCell)row.Cells[0]).Value = true;
}
北渚 2024-07-22 04:45:54

如果它绑定到DataTable,您是否可以不处理模型(表)? 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);
    }
}

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);
    }
}
恏ㄋ傷疤忘ㄋ疼 2024-07-22 04:45:54

大致如下:

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

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;
}
如此安好 2024-07-22 04:45:54

所选行的值不会传递到基础数据源,因此不会保存。 数据源是数据表。 它的数据网格视图问题。

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.

Smile简单爱 2024-07-22 04:45:54
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; }
    }
}

这看起来像是满足您的要求。 我对这一切都很陌生,如果我回答错误,我很抱歉。 只是想帮忙。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文