更改颜色的DataGridViewCell问题 [英] DataGridViewCell issue with changing colors

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

问题描述

我已经在这个相当简单的问题上玩了一段时间了,并决定我只需要其他人来研究它.我有一个带有复选框列的DataGridView.取消选中该框时,我希望该行中的其他单元格为ReadOnly,而BackColor为浅灰色.选中该复选框后,我希望该单元格不为ReadOnly,而BackColor为白色. ReadOnly位工作正常,但我无法更改颜色.当我取消选中一个框时,它会变成浅灰色,并且ReadOnly就好了.但是,如果我选中该框,则它不会变回白色.我输入一些内容,然后离开单元格,然后变成白色.我尝试了几项不同的尝试都无济于事,现在我只是想直截了当地思考.这是我需要的代码:
(colAction是复选框列,conststrRecordedProduction是具有我要更改颜色的单元格名称的字符串常量.)

I''ve been playing around with this fairly simple issue for quite a while and have decided I just need someone else to look at it. I have a DataGridView with a checkbox column in it. When the box is unchecked, I want a different cell in the row to be ReadOnly and the BackColor to be light gray. When it''s checked, I want that cell to not be ReadOnly and the BackColor to be white. The ReadOnly bit is working fine, but I''m having trouble with changing the colors. When I uncheck a box, it turns light gray and ReadOnly just fine. But if I CHECK the box, it doesn''t change back to white. I type something and then leave the cell and THEN it turns white. I tried a couple of different things to no avail and now I''m just to frutrated to think straight. Here is what I''ve got for code:
(colAction is the checkbox column, conststrRecordedProduction is a string constant with the name of the cell that I want to change color.)

Private Sub dgvCommingledProd_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvCommingledProd.CellMouseClick
    If e.Button = Windows.Forms.MouseButtons.Left Then
        If e.RowIndex = -1 Then
            'Header row do nothing
        Else
            'Not header row - make sure just one row is selected
            If dgvCommingledProd.SelectedRows.Count = 1 Then
                'Determine which column was clicked
                If e.ColumnIndex = dgvCommingledProd.Columns(colAction.Name).Index Then
                    'Check or uncheck the box
                    dgvCommingledProd.SelectedRows(0).Cells("colAction").Value = Not dgvCommingledProd.SelectedRows(0).Cells("colAction").Value

                    'If it's not the row header...
                    If e.RowIndex <> -1 Then
                        With dgvCommingledProd.Rows(e.RowIndex)
                            If .Cells(colAction.Name).Value = True Then
                                'User checked the field, set focus to the production column
                                .Cells(conststrRecordedProduction).ReadOnly = False
                                dgvCommingledProd.CurrentCell = dgvCommingledProd.Rows(e.RowIndex).Cells(conststrRecordedProduction)
                            Else
                                'User unchecked the field, clear the value
                                .Cells(conststrRecordedProduction).ReadOnly = True
                                .Cells(conststrRecordedProduction).Value = DBNull.Value
                            End If

                            'Set the cell's color
                            Dim temp As DataGridViewCell = dgvCommingledProd.SelectedRows(0).Cells(conststrRecordedProduction)
                            SetProductionCellStyle(temp)

                        End With
                    End If
                End If
            End If
        End If
    End If
End Sub


Private Sub SetProductionCellStyle(ByRef dgvc As DataGridViewCell)
    If dgvc.ReadOnly Then
        'It's read only, grey it out
        dgvc.Style.BackColor = Color.LightGray
        dgvc.Style.SelectionBackColor = Color.LightGray
        dgvc.Style.ForeColor = Color.Black
        dgvc.Style.SelectionForeColor = Color.Black
    Else
        'Use white & black
        dgvc.Style.BackColor = Color.White
        dgvc.Style.SelectionBackColor = Color.White
        dgvc.Style.ForeColor = Color.Black
        dgvc.Style.SelectionForeColor = Color.Black
    End If
End Sub



我可以在调试时看到它点击了将背景色设置为白色的代码,但是直到稍后它才显示在屏幕上.谁能告诉我正在发生的事情和/或如何解决它?

***更新*** 好的,因此,如果在操作列中选中该框时,如果我注释掉我将当前单元格设置为该列的行,则颜色会改变正常工作.



I can see when debugging that it''s hitting the code to set the backcolor to white, but it''s just not showing on the screen until later. Can anyone tell me what is happening and/or how to work around it?

*** UPDATE ***Okay, so if I comment out the line where I''m setting the current cell to the column when the box is checked in the action column, then the color change seems to work properly.

'Commented out this line:
dgvCommingledProd.CurrentCell = dgvCommingledProd.Rows(e.RowIndex).Cells(conststrRecordedProduction)


所以...也许我的问题应该是如何在不干扰颜色变化的情况下将焦点设置到另一列?


So...maybe my question should be how can I set focus to the other column without interupting the color change?

推荐答案

我认为原因可能是单击CheckBox 单元格,它将位于DirtyState中.当单击其他位置的鼠标时,数据将被提交,并且颜色将更改.要手动强制提交,可以按此处说明的方式处理CurrentCellDirtyStateChanged .

DataGridView.CurrentCellDirtyStateChanged事件 [
I think the reason may be that when the CheckBox cell is clicked it will be in DirtyState. When somewhere else mouse is clicked the data gets committed and the color changes. To force the commit manually the CurrentCellDirtyStateChanged can be handled as explained here.

DataGridView.CurrentCellDirtyStateChanged Event[^]

Please give it a try. I think it may be helpful.


Oi.这个很烦人.我通过将设置当前单元格的代码移到设置颜色的代码后执行来解决了这个问题.一切正常.
Oi. This one was quite annoying. I solved the issue by moving the code that sets the current cell to execute AFTER the code that sets the color. And all works fine.


这篇关于更改颜色的DataGridViewCell问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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