6

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

5 回答 5

11

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

foreach (DataGridViewRow row in dgvDataGridView.Rows)
{
    ((DataGridViewCheckBoxCell)row.Cells[0]).Value = true;
}
于 2009-07-30T08:46:40.987 回答
3

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);
    }
}
于 2009-03-25T10:58:23.660 回答
1

选择其值的行不会传递给底层数据源,因此不会被保存。数据源是数据表。它的datagridview问题。

于 2011-01-26T09:09:56.813 回答
1

类似于以下内容:

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;
}
于 2009-03-25T11:34:42.777 回答
0
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.

于 2017-01-05T17:40:11.663 回答