C# DataGridView Decimal ComboBox 列
我有一个带有绑定组合框列的 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;
有人能指出为什么会出现这种行为吗小数有那么奇怪吗?也许我在这里缺少一些简单的东西?
I have a datagridview with a bound combobox column which contains decimal value. There is a range of valid decimal values so user can choose one of them. However I face a strange problem - when you click on a combobox in the selected value somehow resets to the first one in a list thus losing the one already selected. Look at the pictures
This is the initial view of a window:
This is when I select similar column with a double value in it(note the selection in combobox's list)
This is when I select column with a decimal value. The selection(number 293) is missing
Here is the code I use:
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;
Can someone point out why the behavior is so strange for decimal? Maybe I'm missing something simple here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您收到“奇怪”操作的原因是因为十进制数字不在用于填充列的
DataSource
的列表中。通过将填充 DataTable 的代码更改为以下内容可以解决该问题:
我认为 DataGridView 在列的
DataSource
中的每个元素上调用.ToString()
函数来查找匹配。由于1.0
不等于1
(以字符串形式表示),因此当您打开组合框的下拉菜单时,不会选择任何项目。The reason you're getting that 'strange' operation is because the decimal numbers are not in the list used to populate the column's
DataSource
.The problem is fixed by changing the code that populates the DataTable to this:
I reckon the DataGridView calls the
.ToString()
function on each element in the column'sDataSource
to find the match. Since1.0
does not equal1
(in string terms), no item is selected when you open the drop down menu of the combo box.