DatagridView帮助??? [英] DatagridView Help???
问题描述
我有一个DataGridView,其中有一个名为状态"的列.
如果状态"字段中的值"为"Sls",则该行的BackColor
应变为蓝色.我使用了foreach
循环来更改行的背景色,但是问题是用户可以在DataGridView中添加,编辑,删除行,以及是否在状态字段值为"Sls"的DataGridView中添加新行.则任何行的BackColor
都不会改变.
请帮忙.
是否有一些DataGridView事件会自动检查特定列的值并相应地更改行的颜色?
我的代码是这样的:
I Have a DataGridView in which I have a column named ''Status''.
If Value in the Status field is "Sls" then the BackColor
of the row should become blue. I have used a foreach
loop for changing the BackColor of the Row but problem is that the user can add, edit, delete the rows in DataGridView and if new row is added in the DataGridView with value of the status field = "Sls" then BackColor
of any of the row doesn''t changes.
Please help.
Is there Some event of DataGridView which will automatically check the value of the particular column and will change the color of the row accordingly?
MY Code is like this:
private void dgInvoiceMn_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dgInvoiceMn.Rows[e.RowIndex].Cells["ExtDate"].Value.ToString().Trim() != string.Empty)
{
dgInvoiceMn.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.MediumPurple;
}
if (dgInvoiceMn.Rows[e.RowIndex].Cells["Feeder"].Value.ToString().Trim().ToUpper() == "Y")
{
dgInvoiceMn.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Khaki;
}
if (dgInvoiceMn.Rows[e.RowIndex].Cells["RtnFlg"].Value.ToString().Trim() == "C")
{
dgInvoiceMn.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;
}
if (dgInvoiceMn.Rows[e.RowIndex].Cells["RtnFlg"].Value.ToString().Trim() == "R")
{
dgInvoiceMn.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.HotPink;
}
}
推荐答案
您可以处理CellFormatting
事件,如下所示:
You could possibly handle theCellFormatting
event, something like this:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "Status") //Only do it for 'Status' column
{
if (e.Value != null && e.Value.ToString().ToUpper() == "SLS") // if not null and = 'Sls' converted to upper to allow for typos
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
}
else // not 'sls' so use default
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = dataGridView1.DefaultCellStyle.BackColor;
}
}
}
我尚未对此进行测试,但它应该可以工作
I have not tested this but it should work
我只能建议您调查 ^ ]方法,并将其应用于您的CellFormatting处理程序中.
不能保证,因为我再次对它进行了测试,尽管从描述中它应该强制对Row进行重新绘制.但是要小心,因为它也可能会重新触发CellFormatting,这将触发InvalidateRow等.....,您将得到一个stackoverflow异常.
希望这可以帮助. :)
I can only suggest that you investigate the DataGridView.InvalidateRow()[^] method and apply it within your CellFormatting handler.
No guarantees, as once again I have not tested it although from the description it should force a repaint of the Row. Be careful though as it might also re-trigger the CellFormatting which will trigger the InvalidateRow etc....... and you will get a stackoverflow exception.
Hope this helps. :)
这篇关于DatagridView帮助???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!