C#的DataGridView十进制组合框柱
问题描述:
我有与含有十进制值绑定的组合框列一个DataGridView。有一个有效的小数值范围,因此用户可以选择其中的一个。然而,我面临一个奇怪的问题 - 当你点击一个组合框中的选定值以某种方式重置为列表中的第一个,从而失去已经选择的组合。看图C#的DataGridView十进制组合框柱
这是一个窗口的初始视图:
这是当我选择用它的双精度值(注意,组合框的列表中选择)
这是当我选择用十进制值列。选择(号293)丢失
这里是代码我使用:
public Form1()
{
InitializeComponent();
dgwResult.AutoGenerateColumns = false;
var list = new List<string>(){"A", "B", "C", "D"};
var list2 = new List<double>();
var list3 = new List<decimal>();
for (int i = 0; i < 300; i++)
{
list2.Add((double)i);
list3.Add((decimal)i);
}
dgw_2.DataSource = list;
dgw_2.DataPropertyName = "two";
dgw_3.DataSource = list2;
dgw_3.DataPropertyName = "three";
dgw_4.DataSource = list3;
dgw_4.DataPropertyName = "four";
DataTable dt = new DataTable();
dt.Columns.Add("one", typeof(string));
dt.Columns.Add("two", typeof(string));
dt.Columns.Add("three", typeof(double));
dt.Columns.Add("four", typeof(decimal));
dt.Rows.Add(new object[] { "akjsgdf", "A", 10.0, 10.0m });
dt.Rows.Add(new object[] { "akjsgdf", "B", 15.0, 15.0m });
dt.Rows.Add(new object[] { "akjsgdf", "C", 20.0, 20.0m });
dt.Rows.Add(new object[] { "akjsgdf", "D", 15.0, 15.0m });
dt.Rows.Add(new object[] { "akjsgdf", "C", 293.0, 293.0m });
dgwResult.DataSource = dt;
}
private void InitializeComponent()
{
this.dgwResult = new System.Windows.Forms.DataGridView();
this.dgw_1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.dgw_2 = new System.Windows.Forms.DataGridViewComboBoxColumn();
this.dgw_3 = new System.Windows.Forms.DataGridViewComboBoxColumn();
this.dgw_4 = new System.Windows.Forms.DataGridViewComboBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.dgwResult)).BeginInit();
this.SuspendLayout();
//
// dgwResult
//
this.dgwResult.AllowUserToAddRows = false;
this.dgwResult.AllowUserToDeleteRows = false;
this.dgwResult.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
this.dgwResult.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgwResult.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.dgw_1,
this.dgw_2,
this.dgw_3,
this.dgw_4});
this.dgwResult.Location = new System.Drawing.Point(12, 12);
this.dgwResult.Name = "dgwResult";
this.dgwResult.RowHeadersVisible = false;
this.dgwResult.Size = new System.Drawing.Size(268, 150);
this.dgwResult.TabIndex = 0;
this.dgwResult.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgwResult_CellClick);
//
// dgw_1
//
this.dgw_1.DataPropertyName = "one";
this.dgw_1.HeaderText = "One";
this.dgw_1.Name = "dgw_1";
//
// dgw_2
//
this.dgw_2.DataPropertyName = "two";
this.dgw_2.HeaderText = "Two";
this.dgw_2.Name = "dgw_2";
//
// dgw_3
//
this.dgw_3.DataPropertyName = "three";
this.dgw_3.HeaderText = "Double";
this.dgw_3.Name = "dgw_3";
//
// dgw_4
//
this.dgw_4.DataPropertyName = "four";
this.dgw_4.HeaderText = "Decimal";
this.dgw_4.Name = "dgw_4";
this.dgw_4.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.dgw_4.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.dgwResult);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dgwResult)).EndInit();
this.ResumeLayout(false);
}
private System.Windows.Forms.DataGridView dgwResult;
private System.Windows.Forms.DataGridViewTextBoxColumn dgw_1;
private System.Windows.Forms.DataGridViewComboBoxColumn dgw_2;
private System.Windows.Forms.DataGridViewComboBoxColumn dgw_3;
private System.Windows.Forms.DataGridViewComboBoxColumn dgw_4;
有人能指出为什么的行为是如此奇怪的小数?也许我在这里错过了简单的东西?
答
你得到的是“奇怪”操作的原因是小数号码不是用来填充列表列的DataSource
。
问题是通过改变填充的DataTable这个代码固定:
dt.Rows.Add(new object[] { "akjsgdf", "A", 10.0, 10m });
dt.Rows.Add(new object[] { "akjsgdf", "B", 15.0, 15m });
dt.Rows.Add(new object[] { "akjsgdf", "C", 20.0, 20m });
dt.Rows.Add(new object[] { "akjsgdf", "D", 15.0, 15m });
dt.Rows.Add(new object[] { "akjsgdf", "C", 293.0, 293m });
我想在DataGridView调用列的DataSource
找到匹配各元素.ToString()
功能。由于1.0
不等于1
(字符串而言),没有项目,当您打开的下拉组合框中的菜单中选择。
@ V4Vendetta:它不会在这种情况下,在所有绑定。我注意到有趣的事情 - 如果我没有指定裂缝部分(即293m而不是293.0m),它可以正常工作。 – Blablablaster