如何获得“真实”的信息? DataRow 中单元格后面的对象
我想将自定义对象与 DataTable
的 DataRow
中的每个单元格关联起来,以便在从 DataGridView 获取的事件上,我可以自定义颜色和其他行为。因此,当我添加新行时,我执行以下操作:
DataRow oRow = dtItens.NewRow();
oRow["CodFamilia"] = new ClsCelula(TipoCelula.tcMostrar, "", Color.White);
oRow["Familia"] = new ClsCelula(TipoCelula.tcMostrar, "", Color.White);
oRow["Item"] = new ClsCelula(TipoCelula.tcMostrar, "", Color.White);
oRow["Descricao"] = new ClsCelula(TipoCelula.tcMostrar, "", Color.White);
oRow["Referencia"] = new ClsCelula(TipoCelula.tcMostrar, "Saldo Inicial", Color.Aqua);
dtItens.Rows.Add(oRow);
在 DataGridView 的 CellFormatting 事件中,我想让 ClsCelula 对象读取它的属性,如下所示:
Object oCelula = dtItens.Rows[e.RowIndex][e.ColumnIndex];
if (oCelula != null)
{
if (oCelula is ClsCelula)
{
ClsCelula oValorCelula = (ClsCelula)oCelula;
e.CellStyle.BackColor = oValorCelula.Cor;
}
}
但是,这不起作用,因为代码可能正在调用 < code>ToString() 当我读取行/列索引时,因此 oCelula 始终是 System.String
。有什么办法解决这个问题吗?如何访问“真实”对象?
I wanna associate a custom object to each cell in a DataTable
's DataRow
so that, on the events I get from the DataGridView, I can customize coloring and other behavior. So, when I add a new row, I do the following:
DataRow oRow = dtItens.NewRow();
oRow["CodFamilia"] = new ClsCelula(TipoCelula.tcMostrar, "", Color.White);
oRow["Familia"] = new ClsCelula(TipoCelula.tcMostrar, "", Color.White);
oRow["Item"] = new ClsCelula(TipoCelula.tcMostrar, "", Color.White);
oRow["Descricao"] = new ClsCelula(TipoCelula.tcMostrar, "", Color.White);
oRow["Referencia"] = new ClsCelula(TipoCelula.tcMostrar, "Saldo Inicial", Color.Aqua);
dtItens.Rows.Add(oRow);
On the DataGridView's CellFormatting event, I want to get my ClsCelula object to read it's properties, like below:
Object oCelula = dtItens.Rows[e.RowIndex][e.ColumnIndex];
if (oCelula != null)
{
if (oCelula is ClsCelula)
{
ClsCelula oValorCelula = (ClsCelula)oCelula;
e.CellStyle.BackColor = oValorCelula.Cor;
}
}
However, this doesn't work, since probably the code is calling ToString()
when I read the Row/Column index, so oCelula is always a System.String
. Is there any way around this? How can I access the "real" object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您正在使用对象模型,因此似乎不需要在此处使用 all
DataTable
- 只需将DataSource
设置为>List
(或更好:BindingList
)然后就可以了!DataGridView
非常高兴地绑定到对象,并且底层对象只是.DataBoundItem
在每一行上。注意 - 对于双向数据绑定(即,如果您希望在直接通过代码编辑对象时更新网格),您可能需要使用
BindingList
和 实现 INotifyPropertyChanged - 但如果您只想显示列表并通过网格编辑项目,则没有必要。Since you are working from an object model, there seems to be no need to use
DataTable
here all - just set theDataSource
to aList<T>
(or better:BindingList<T>
) and away you go!DataGridView
is perfectly happy binding to objects, and the underlying object is just.DataBoundItem
on each row.Note - for two-way data-binding (i.e. If you want the grid to update when you edit an object directly through code) you might want to use
BindingList<T>
and implementINotifyPropertyChanged
- but this isn't necessary if you just want to display a list and edit items via the grid.这里有几个选项:
DataColumn
对象创建DataTable
,并将每列对象类型指定为ClsCelula
。在这种情况下,将其格式化为网格时会遇到问题。Dictionary
等中,其中索引将是您将拥有的某种自动增量数字创建和维护。Several options here:
DataTable
from your ownDataColumn
objects, and specify the each column object type asClsCelula
. You'll have problems formatting it for the grid, in this case.Dictionary<int, ClsCelula>
or such, where index would be some kind of autoincrement number you will have to create and maintain.我在代码中发现了这样的东西:
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value
。您可能必须将其转换为特定类型。I found such thing in my code:
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value
. You might have to cast that to a particular type.老问题,但无论如何:
Old question, but anyway: