有一个奇怪的问题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;
}