0

我遇到此問題。它開始是這樣的:向我的DataGridView添加新行時出現異常

Begin

我可以雙擊下拉箭頭,選擇一種顏色OK:

Pick colour

在這一點上,我可以再次點擊下拉菜單,它是正確選擇:

Selected

的問題是,當我想牛逼o添加一個新行。即使我只是去點擊新建行單元格:

Click new row

我則數據的數據錯誤異常:

Exception

之後與前一單元繪製錯誤:

enter image description here

我不能完全解決如何解決這個問題。

我的代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Xml.Linq; 
using Teigha.Core; 
using Teigha.TD; 

namespace Viewer 
{ 
    public partial class Editor : Form 
    { 
     uint[] _CurPalette = Teigha.Core.AllPalettes.getDarkPalette(); 

     public Editor() 
     { 
      InitializeComponent(); 
     } 

     private void Editor_Load(object sender, EventArgs e) 
     { 
      DataGridViewComboBoxColumn cboColumn = new DataGridViewComboBoxColumn(); 
      cboColumn.Name = "Color"; 

      List<ushort> listColors = new List<ushort>(); 
      listColors.Add(1); 
      listColors.Add(2); 
      listColors.Add(3); 
      listColors.Add(4); 
      listColors.Add(5); 
      listColors.Add(6); 
      listColors.Add(7); 
      listColors.Add(8); 
      listColors.Add(9); 
      listColors.Add(250); 
      listColors.Add(251); 
      listColors.Add(252); 
      listColors.Add(253); 
      listColors.Add(254); 
      listColors.Add(255); 

      foreach (ushort iColorIndex in listColors) 
      { 
       using (OdCmColor oColor = new OdCmColor()) 
       { 
        oColor.setColorIndex(iColorIndex); 
        ComboboxColorItem oColorItem = new ComboboxColorItem(
         oColor.colorNameForDisplay(), 
         iColorIndex, 
         Color.FromArgb(oColor.red(), oColor.green(), oColor.blue())); 
        cboColumn.Items.Add(oColorItem); 
       } 
      } 

      this.DataGridView1.Columns.Add(cboColumn); 
     } 

     private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
     { 
      if (e.Control is ComboBox) 
      { 
       ComboBox theCB = (ComboBox)e.Control; 
       theCB.DrawMode = DrawMode.OwnerDrawFixed; 
       try 
       { 
        theCB.DrawItem -= new DrawItemEventHandler(this.combobox1_DrawItem); 
       } 
       catch { } 
       theCB.DrawItem += new DrawItemEventHandler(this.combobox1_DrawItem); 
      } 
     } 

     private void combobox1_DrawItem(object sender, DrawItemEventArgs e) 
     { 
      Graphics g = e.Graphics; 
      Color c = Color.Empty; 
      string s = ""; 
      Brush br = SystemBrushes.WindowText; 
      Brush brBack; 
      Rectangle rDraw; 
      bool bSelected = Convert.ToBoolean(e.State & DrawItemState.Selected); 
      bool bValue = Convert.ToBoolean(e.State & DrawItemState.ComboBoxEdit); 

      rDraw = e.Bounds; 
      rDraw.Inflate(-1, -1); 

      if (bSelected & !bValue) 
      { 
       brBack = Brushes.LightBlue; 
       g.FillRectangle(Brushes.LightBlue, rDraw); 
       g.DrawRectangle(Pens.Blue, rDraw); 
      } 
      else 
      { 
       brBack = Brushes.White; 
       g.FillRectangle(brBack, e.Bounds); 
      } 

      try 
      { 
       //s = ((ComboBox)sender).Items[e.Index].ToString(); 
       ComboboxColorItem oColorItem = (ComboboxColorItem)((ComboBox)sender).Items[e.Index]; 
       s = oColorItem.ToString(); 
       c = oColorItem.Value; 
      } 
      catch 
      { 
       s = "red"; 
       c = Color.Red; 
      } 

      SolidBrush b = new SolidBrush(c); 
      Rectangle r = new Rectangle(e.Bounds.Left + 5, e.Bounds.Top + 3, 10, 10); 
      g.FillRectangle(b, r); 
      g.DrawRectangle(Pens.Black, r); 
      g.DrawString(s, Form.DefaultFont, Brushes.Black, e.Bounds.Left + 25, e.Bounds.Top + 1); 

      b.Dispose(); 
      g.Dispose(); 
     } 

     private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) 
     { 
      MessageBox.Show(e.Exception.ToString()); 
     } 
    } 

    public class ComboboxColorItem 
    { 
     public string Name { get; } 
     public ushort Index { get; } 
     public Color Value { get; } 

     public ComboboxColorItem(string Name, ushort Index, Color Value) 
     { 
      this.Name = Name; 
      this.Index = Index; 
      this.Value = Value; 
     } 
     public override string ToString() 
     { 
      return Name; 
     } 
    } 
} 

更新:

如果我把這個代碼到窗體加載事件:

this.DataGridView1.Rows.Add(new ComboboxColorItem("red", 1, Color.Red)); 

然後我立即得到例外。

我嘗試添加輔助默認的構造函數,它並沒有區別:

public class ComboboxColorItem 
{ 
    public string Name { get; set; } 
    public ushort Index { get; set; } 
    public Color Value { get; set; } 

    public ComboboxColorItem() 
    { 
     this.Name = "red"; 
     this.Index = 1; 
     this.Value = Color.Red; 
    } 
    public ComboboxColorItem(string Name, ushort Index, Color Value) 
    { 
     this.Name = Name; 
     this.Index = Index; 
     this.Value = Value; 
    } 
    public override string ToString() 
    { 
     return Name; 
    } 
} 

我也嘗試添加該負載事件:

cboColumn.ValueType = typeof(ComboboxColorItem); 

沒有有所作爲。

更新:

我已經使用了CellParsing答案,我似乎不再給予崩潰:

CellParsing results

我唯一的評論是現在,我希望該顏色的塊將保持在細胞中可見。但是當我點擊下拉箭頭時,我只能看到顏色塊。這是一個單獨的問題嗎?

回答

1

DataGridView與解析故障選定的顏色項目。我建議您將自定義解析邏輯應用於該列:

DataGridView1.CellParsing += ColorCellParsing; 

private void ColorCellParsing(object sender, DataGridViewCellParsingEventArgs e) 
{ 
    var grid = (DataGridView)sender; 
    var column = grid.Columns[e.ColumnIndex] as DataGridViewComboBoxColumn; 
    if (column == null || column.Name != "Color") 
     return; 
    foreach (ComboboxColorItem item in column.Items) 
    { 
     if (item.Name == (string) e.Value) 
     { 
      e.Value = item; 
      e.ParsingApplied = true; 
      break; 
     }  
    } 
} 
+0

)謝謝,這也似乎不起作用,我認爲它起初有幫助 –

+0

我注意到在你的例子和我的調試中,我們只是看文本值,但我們也有實際的Color對象。我們解析一下嗎? –

+0

@AndrewTruckle,顏色名稱是'e.Value'返回(由於ToString()的實現),但是可以改變ValueMember的列'cboColumn.ValueMember =「Index」;' – ASh

0

如果你想獲得比字符串以外的東西,設置列的ValueTypetypeof(<data type>)
例如如果是INT

this.gridViewSettings.Columns("columnName").ValueType= typeof(Int32); 

this.ComboboxCellcolumnName.ValueType = typeof(int); 
+0

謝謝。我只是嘗試擊中明星並得到相同的錯誤。 –

+0

謝謝。我已經嘗試'cboColumn.ValueType = typeof(Color);'和'cboColumn.ValueType = typeof(ComboboxColorItem);'這似乎沒有任何區別。 ( –

相關問題