DatagridView帮助??? [英] DatagridView Help???

查看:72
本文介绍了DatagridView帮助???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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 the CellFormatting 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


我只能建议您调查
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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆