【问题标题】:How to Change background color of the cell if the cell is being edited如果正在编辑单元格,如何更改单元格的背景颜色
【发布时间】:2015-08-30 23:54:56
【问题描述】:

我有两个 datagridview 单元格,grid1 和 grid2。我是否将文件加载到两个网格中,我使用了文件的版本。每次我保存/编辑文件时,它都会将文件版本从 5 增加 1,这意味着它将是 6、7、8、9 等。

文件版本从 5 开始,增量是没有问题的

如果我加载文件并编辑,我想更改单元格的背景颜色,从编辑的那个单元格中,它必须将背景颜色更改为黄色。

  1. 加载现有文件
  2. 编辑文件
  3. 已编辑单元格的背景颜色必须变为黄色
  4. 保存文件并清除颜色(这是有效的)

我已经尝试过了,但是当我创建文件时它会突出显示颜色。我只需要在编辑文件时更改背景单元格颜色。

我的代码:

    int version_Number = 5;
    string _OriginalValue;
    private void Grid1_CellBeginEdit_1(object sender, DataGridViewCellCancelEventArgs e)
    {
        try
        {
            _OriginalValue = Grid1[e.ColumnIndex, e.RowIndex].Value.ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error occured.\nError message: " + ex.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }


    #region Grid2_CellEndEdit_1
    private void Grid2_CellEndEdit_1(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            DataGridViewCell cell = Grid2[e.ColumnIndex, e.RowIndex];
            if (cell.Value.ToString() != _OriginalValue)
            {
                if (version_Number >= 1000)
                {
                    cell.Style.BackColor = Color.Yellow;
                }
            }   

【问题讨论】:

  • 不时使用旗帜并不丢人。在加载数据之前将其设置为 bool loading=true,然后设置为 false。在CellEndEdit 事件中测试它!
  • 你能告诉我怎么做吗?
  • 我的回答假设您的着色代码正在工作,除了加载问题..
  • 您的问题解决了吗?

标签: c# winforms datagridview background-color


【解决方案1】:

您必须在单元格格式化事件中处理为单元格着色的部分,我尝试的示例如下

 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
            if (cell.Value  != null)
            {
                if (cell.Value.ToString() != _OriginalValue)
                {

                    cell.Style.BackColor = Color.Yellow;

                }   
            }

    }

【讨论】:

    【解决方案2】:

    您可能希望使用一个标志来阻止在加载数据期间发生着色:

    int version_Number = 5;
    string _OriginalValue;
    
    bool loading = false;
    

    您在某处加载数据;现在在那里设置和重置标志:

    loding = true;
    yourDataLoadingCodeHere;
    loading = false;
    

    现在,如果它们正常工作,您可以简单地中止您的两个事件:

    private void Grid1_CellBeginEdit_1(object sender, DataGridViewCellCancelEventArgs e)
    {
        if (loading) return;
    
        try
        {
            _OriginalValue = Grid1[e.ColumnIndex, e.RowIndex].Value.ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error occured.\nError message: " + 
            ex.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    
    
    
    private void Grid2_CellEndEdit_1(object sender, DataGridViewCellEventArgs e)
    {
       if (loading) return;
    
       try
       {
         DataGridViewCell cell = Grid2[e.ColumnIndex, e.RowIndex];
         if (cell.Value.ToString() != _OriginalValue)
         {
            if (version_Number >= 1000)
            {
               cell.Style.BackColor = Color.Yellow;
            }
        }  
        ..
        ..
    

    【讨论】:

      猜你喜欢
      • 2012-12-05
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-03
      • 2019-12-13
      • 2011-09-24
      相关资源
      最近更新 更多