6

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

5 に答える 5

11

以下は私のために働いた、それはチェックボックスを完全にチェックした:)

foreach (DataGridViewRow row in dgvDataGridView.Rows)
{
    ((DataGridViewCheckBoxCell)row.Cells[0]).Value = true;
}
于 2009-07-30T08:46:40.987 に答える
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 に答える
1

選択された行の値は、基になるデータソースに渡されないため、保存されません。データソースは Datatable です。データグリッドビューの問題。

于 2011-01-26T09:09:56.813 に答える
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; }
    }
}

これは、あなたが必要とすることをしているようです。私はこれらすべてに慣れていないので、間違って答えていたらごめんなさい。助けようとしているだけです。

于 2017-01-05T17:40:11.663 に答える