(DataGridView + 绑定)如何根据绑定的对象对线条进行着色?
我想根据绑定对象的属性为特定行添加背景色。
我拥有的解决方案(并且有效)是使用事件 DataBindingComplete ,但我认为这不是最好的解决方案。
事件如下:
private void myGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
for (int i = 0; i < this.myGrid.Rows.Count; i++)
{
if((this.myGrid.Rows[i].DataBoundItem as MyObject).Special)
{
this.myGrid.Rows[i].DefaultCellStyle.BackColor = Color.FromArgb(240, 128, 128);
}
}
}
还有其他更好的选择吗?
I would like to add a backcolor for specific line depending of a Property of the object binded.
The solution I have (and it works) is to use the Event DataBindingComplete
but I do not think it's the best solution.
Here is the event:
private void myGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
for (int i = 0; i < this.myGrid.Rows.Count; i++)
{
if((this.myGrid.Rows[i].DataBoundItem as MyObject).Special)
{
this.myGrid.Rows[i].DefaultCellStyle.BackColor = Color.FromArgb(240, 128, 128);
}
}
}
Any other option that would be better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以将事件处理程序附加到 RowPostPaint:
You can also attach an event handler to RowPostPaint:
我实际上不太使用 WinForms,但在 ASP 中,您会使用“ItemDataBound”方法。 Windows 窗体中是否有类似的 DataGrid?
如果是这样,在该方法中,事件参数将包含数据绑定的项目以及 DataGrid 行。 所以一般代码看起来像这样(语法可能是关闭的):
I don't really work with WinForms that much, but in ASP you would use the 'ItemDataBound' method. Is there something similar in windows forms for a DataGrid?
If so, in that method, the event arguments would contain the item that was databound, along with the DataGrid row. So the general code would look something like this (syntax is probably off):
我建议一些事情:
如果您在实施过程中遇到困难,请告诉我,我将发布一个片段。
I would suggest a few things:
Let me know if you struggle with the implementation and i'll post a snippet.