DataGridView,DataGridViewCheckBoxColumn不允许多次检查

时间:2013-11-16 13:27:36

标签: c# checkbox datagridview datagridviewcheckboxcell

DataGridView有一个奇怪的问题。 其DataSource绑定到set; get;的对象列表 它获取来自此源的数据,因为它应该具有datagridview中复选框的所有布尔状态。 但是,当我尝试更改DataGridView中的复选框时,它一次只能检查一个复选框。有点像单选按钮。

这不是我追求的功能,因为我希望能够检查多个复选框,然后可能保存已经完成的更改以使其永久化。

Iv做了两次这个DataGridView,因为我认为它可能是一些可能包含的神秘环境。

知道可能导致这种情况的原因吗?

她提供了一些代码。

仅为测试

以常规形式设置DGV
 public partial class Form1 : Form
{

    /// <summary>
    /// 
    /// </summary>
    public struct TestObject
    {
        public string text { set; get; }

        public bool cbOne { set; get; }

        public bool cbTwo { set; get; }
    }

    public Form1()
    {
        InitializeComponent();

        SetupDS();
    }

    private void SetupDS()
    {
        dataGridView1.DataSource = SetupDBdata();
    }


    private List<TestObject> SetupDBdata()
    {
        List<TestObject> list = new List<TestObject>();

        for (int i = 0; i < 10; i++)
        {
            list.Add(new TestObject() { text = i.ToString() });
        }

        return list;
    }

    private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        if (dataGridView1.IsCurrentCellDirty)
        {
            dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }
}

设计代码

    partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.txt = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.cb1 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
        this.cb2 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.SuspendLayout();
        // 
        // dataGridView1
        // 
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.txt,
        this.cb1,
        this.cb2});
        this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.dataGridView1.Location = new System.Drawing.Point(0, 0);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(651, 390);
        this.dataGridView1.TabIndex = 0;
        this.dataGridView1.CurrentCellDirtyStateChanged += new System.EventHandler(this.dataGridView1_CurrentCellDirtyStateChanged);
        // 
        // txt
        // 
        this.txt.DataPropertyName = "text";
        this.txt.HeaderText = "text";
        this.txt.Name = "txt";
        // 
        // cb1
        // 
        this.cb1.DataPropertyName = "cbOne";
        this.cb1.HeaderText = "cb1";
        this.cb1.Name = "cb1";
        // 
        // cb2
        // 
        this.cb2.DataPropertyName = "cbTwo";
        this.cb2.HeaderText = "cb2";
        this.cb2.Name = "cb2";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(651, 390);
        this.Controls.Add(this.dataGridView1);
        this.Name = "Form1";
        this.Text = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.DataGridView dataGridView1;
    private System.Windows.Forms.DataGridViewTextBoxColumn txt;
    private System.Windows.Forms.DataGridViewCheckBoxColumn cb1;
    private System.Windows.Forms.DataGridViewCheckBoxColumn cb2;
}

1 个答案:

答案 0 :(得分:1)

您的问题是您使用struct作为基础数据绑定项。您对记录所做的所有修改都将应用于基础项目的副本。你应该改用一个类:

public class TestObject
{
  //....
}

基础机制将通过List索引器获取项目,您知道indexer的{​​{1}}可能是这样的:

List<TestObject>

public TestObject this[int index] { get { return someValue;} set { ... } } 将是您的someValue实例,并且您知道返回值只是实际结构的副本。因此,副本上的所有更改都不会反映到实际的结构。您可以使用以下代码对列表进行简单测试:

struct

该错误是通过设计通知的,更改副本的数据成员意味着什么,所以只需通知一些错误,以防止程序员编译无用的代码。