0

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

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

我做了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;
}
4

1 回答 1

1

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

public class TestObject
{
  //....
}

底层机制将通过您的索引器获取项目,List知道indexer您的List<TestObject>可以是这样的:

public TestObject this[int index] {
  get { return someValue;}
  set { ... }
}

someValue将是您的struct实例,并且您知道返回值只是实际结构的副本。因此,副本上的所有更改都不会反映到实际结构中。可以使用以下代码对您的列表进行简单测试:

yourList[0].text = "???"; //<--- error saying Cannot modify the return value of ... 
                          //because it is not a variable.

该错误是设计通知的,更改副本的数据成员没有任何意义,因此只需通知一些错误以防止程序员编译无用的代码。

于 2013-11-16T17:05:01.157 回答