2011-11-16 67 views
1

我有與含有十進制值綁定的組合框列一個DataGridView。有一個有效的小數值範圍,因此用戶可以選擇其中的一個。然而,我面臨一個奇怪的問題 - 當你點擊一個組合框中的選定值以某種方式重置爲列表中的第一個,從而失去已經選擇的組合。看圖C#的DataGridView十進制組合框柱

這是一個窗口的初始視圖:

enter image description here

這是當我選擇用它的雙精度值(注意,組合框的列表中選擇) enter image description here

類似列

這是當我選擇用十進制值列。選擇(號293)丟失

enter image description here

這裏是代碼我使用:

 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; 

有人能指出爲什麼的行爲是如此奇怪的小數?也許我在這裏錯過了簡單的東西?

+0

@ V4Vendetta:它不會在這種情況下,在所有綁定。我注意到有趣的事情 - 如果我沒有指定裂縫部分(即293m而不是293.0m),它可以正常工作。 – Blablablaster

回答

0

你得到的是「奇怪」操作的原因是小數號碼不是用來填充列表列的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(字符串而言),沒有項目,當您打開的下拉組合框中的菜單中選擇。