将 DataGridViewComboBoxColumn 项转换为另一个对象失败
Casting a DataGridViewComboBoxColumn item into another object fails
此方法是我从 DataGridViewComboBoxColumn
:
派生的 class 的一部分
public ComboboxColourItem InsertColour(ushort iColourIndex)
{
ComboboxColourItem ocbItem = ComboboxColourItem.Create(iColourIndex);
bool bAppend = true;
if (Items.Count > 15)
{
// There are other colours, need to find right index
for(int i = 15; i < Items.Count; i++)
{
//if(ocbItem.Index < (ComboboxColourItem)Items[i].Index)
//{
//}
ComboboxColourItem ocbItem2 = (ComboboxColourItem)Items[i];
if (ocbItem.Index < ocbItem2.Index)
{
bAppend = false;
Items.Insert(i, ocbItem);
break;
}
}
}
if (bAppend)
Items.Add(ocbItem);
return ocbItem;
}
Items
包含 ComboboxColourItem
个对象。以下是这些项目的定义:
public class ComboboxColourItem
{
public string Name { get; set; }
public ushort Index { get; set; }
public Color Value { get; set; }
public ComboboxColourItem(string Name, ushort Index, Color Value)
{
this.Name = Name;
this.Index = Index;
this.Value = Value;
}
public override string ToString()
{
return Name;
}
static public ComboboxColourItem Create(ushort iColourIndex)
{
OdCmColor oColour = new OdCmColor();
oColour.setColorIndex(iColourIndex);
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
String strColour = textInfo.ToTitleCase(oColour.colorNameForDisplay());
if (iColourIndex < 8)
strColour = String.Format("{0} ({1})", strColour, iColourIndex);
else if (iColourIndex == 8 || iColourIndex == 9 || iColourIndex >= 250)
strColour = String.Format("Grey Shade ({0})", iColourIndex);
else
strColour = String.Format("Other ({0})", iColourIndex);
ComboboxColourItem oColourItem = new ComboboxColourItem(
strColour,
iColourIndex,
Color.FromArgb(oColour.red(), oColour.green(), oColour.blue()));
oColour.Dispose();
return oColourItem;
}
}
我知道我可以使用foreach(ComboboxColourItem ocbItem2 in Items)
但是我需要从某个索引开始。所以我决定使用常规 for
循环。
我认为我可以这样做来从 object
:
中投射物品
if(ocbItem.Index < (ComboboxColourItem)Items[i].Index)
{
}
不喜欢演员表。然而,如果我这样做:
ComboboxColourItem ocbItem2 = (ComboboxColourItem)Items[i];
if (ocbItem.Index < ocbItem2.Index)
{
}
效果很好。那么为什么我不能施法呢?我做错了吗?我不认为我可以在这种情况下使用 foreach
。
由于成员访问的优先级高于强制转换(C# operator precedence),以下
(ComboboxColourItem)Items[i].Index
等同于
(ComboboxColourItem)(Items[i].Index)
这当然是无效的。
改用这个
((ComboboxColourItem)Items[i]).Index
此方法是我从 DataGridViewComboBoxColumn
:
public ComboboxColourItem InsertColour(ushort iColourIndex)
{
ComboboxColourItem ocbItem = ComboboxColourItem.Create(iColourIndex);
bool bAppend = true;
if (Items.Count > 15)
{
// There are other colours, need to find right index
for(int i = 15; i < Items.Count; i++)
{
//if(ocbItem.Index < (ComboboxColourItem)Items[i].Index)
//{
//}
ComboboxColourItem ocbItem2 = (ComboboxColourItem)Items[i];
if (ocbItem.Index < ocbItem2.Index)
{
bAppend = false;
Items.Insert(i, ocbItem);
break;
}
}
}
if (bAppend)
Items.Add(ocbItem);
return ocbItem;
}
Items
包含 ComboboxColourItem
个对象。以下是这些项目的定义:
public class ComboboxColourItem
{
public string Name { get; set; }
public ushort Index { get; set; }
public Color Value { get; set; }
public ComboboxColourItem(string Name, ushort Index, Color Value)
{
this.Name = Name;
this.Index = Index;
this.Value = Value;
}
public override string ToString()
{
return Name;
}
static public ComboboxColourItem Create(ushort iColourIndex)
{
OdCmColor oColour = new OdCmColor();
oColour.setColorIndex(iColourIndex);
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
String strColour = textInfo.ToTitleCase(oColour.colorNameForDisplay());
if (iColourIndex < 8)
strColour = String.Format("{0} ({1})", strColour, iColourIndex);
else if (iColourIndex == 8 || iColourIndex == 9 || iColourIndex >= 250)
strColour = String.Format("Grey Shade ({0})", iColourIndex);
else
strColour = String.Format("Other ({0})", iColourIndex);
ComboboxColourItem oColourItem = new ComboboxColourItem(
strColour,
iColourIndex,
Color.FromArgb(oColour.red(), oColour.green(), oColour.blue()));
oColour.Dispose();
return oColourItem;
}
}
我知道我可以使用foreach(ComboboxColourItem ocbItem2 in Items)
但是我需要从某个索引开始。所以我决定使用常规 for
循环。
我认为我可以这样做来从 object
:
if(ocbItem.Index < (ComboboxColourItem)Items[i].Index)
{
}
不喜欢演员表。然而,如果我这样做:
ComboboxColourItem ocbItem2 = (ComboboxColourItem)Items[i];
if (ocbItem.Index < ocbItem2.Index)
{
}
效果很好。那么为什么我不能施法呢?我做错了吗?我不认为我可以在这种情况下使用 foreach
。
由于成员访问的优先级高于强制转换(C# operator precedence),以下
(ComboboxColourItem)Items[i].Index
等同于
(ComboboxColourItem)(Items[i].Index)
这当然是无效的。
改用这个
((ComboboxColourItem)Items[i]).Index