DataGridView-如果特定值,则更改单元格颜色 [英] DataGridView - Change Cell Color if Particular Value

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

问题描述

我在datagrid视图中有一堆列(日期,交易类型,金额).

I have a bunch of columns in a datagrid view (date, transaction type, amount).

如果连续的金额为'150.00',我想将该单元格设为红色.

If the amount in a row is '150.00', I want to make that cell red.

我制作了一个Private Sub并在填充datagridview之后运行它.

I made a Private Sub and run this after the datagridview is filled.

Private Sub ColorCells()

    Dim ColumnCount As Integer
    ColumnCount = dgvLedger.ColumnCount - 1
    Dim ColumnSelected As Integer = 5
    Dim ColumnLoop As Integer
    ' MessageBox.Show("" & ColumnCount & "")

    Try
      For ColumnLoop = 0 To ColumnCount



        Dim CountInt32 As Int32
        Dim CellValueString As String
        Try
          For CountInt32 = 0 To dgvLedger.Rows.Count - 1
            CellValueString = dgvLedger.Rows(CountInt32).Cells(ColumnSelected).Value.ToString
            'MessageBox.Show("" & CellValueString & "")

            dgvLedger.Columns(ColumnSelected).DefaultCellStyle.BackColor = Color.Lime
            dgvLedger.Columns(ColumnSelected).DefaultCellStyle.ForeColor = Color.Black
            MessageBox.Show(CellValueString)


            If CellValueString = "150.00" Then
              dgvLedger.Rows(CountInt32).Cells(ColumnSelected).Style.BackColor = Color.Red
            End If
          Next CountInt32
        Catch ex As Exception
          '   pErrorMessageString = ex.Message
        End Try

        ColumnSelected = ColumnSelected + 1

      Next ColumnLoop

    Catch ex As Exception
      'Fail

    End Try


  End Sub


但是,它似乎有两个错误.


However it seems to have two bugs. 

在第一次加载表单时第一次运行它时,它不会将红色的150.00行放在其中.

When it is first run the first time when the form is loaded, it doesn't put the rows with 150.00 in it in red.

如果我在表单上有一个按钮并将其设置为运行此Private Sub,则它将再次运行它,并将150中的颜色着色为红色.

If I have a button on the form and set it to run this Private Sub, then it runs through it again and will color the ones in 150 as red.

如果我通过单击一列来重用datagridview,它们将再次变为绿色,从而失去了格式.

If I resort the datagridview by clicking on a column, they all return to green again, and it looses the formatting.

有什么办法可以解决这两个错误?

Any way to resolve these two bugs?

 

谢谢.

 

 

 

推荐答案

datagridview有时在格式化时会比较棘手.我在上一个应用程序中也遇到了一些麻烦.必须在上面贴上一些创可贴.我今天发现,我将需要返回并重做许多使用datagridview的操作, 纠正问题.

The datagridview can be tricky with formatting at times.  i have had some trouble in my last app as well.  had to put a few band-aids on it.  and i found out today that i will need to go back and redo much of the use of the datagridview to correct the issues.

除了我的问题,我的建议是:

aside from my issues, what i suggest is this:

尝试在循环之前设置列的默认单元格样式,并且仅设置一种颜色.或者您可以选择将单个单元格底色设置为循环.首先检查您的150值(如果找到),然后设置红色,然后使用"continue for"(继续). 移到下一行.

try setting the defaultcellstyle of your column before the loop, and only set one color.  or you may choose to set individual cell backcolors as your loop.  check first for you 150 value, if found then set the red color and then use "continue for" to move past to the next row.

在最初未设置的情况下,可能取决于调用代码的时间.您是从表单加载事件中调用它的吗?在尝试设置颜色之前是否已经填充了网格?

as far as it not setting initially, might depend on when you are calling the code.  are you calling it from the form load event, and is the grid already populated before you try to set the colors?

排序可能是个问题,因为更改行的列表结构可能会导致格式重置.这类似于我在向datagridview中添加列时遇到的问题.由于某种原因,它会丢失所有格式. 我很确定我的问题是由于将datagridview托管在面板中,但尚未在外部进行测试.不确定datagridview是否应该达到应有的水平.似乎最好用较少的格式.

the sort may be an issue because changes to the list structure of the rows may cause resetting of the formats.  This is similar to the issue i have been having with adding columns to a datagridview.  for some reason it loses all formatting.  I am pretty sure my issue is because of hosting the datagridview in a panel but have not tested it outside yet.  Not sure the datagridview is as good as it should be.  seems to do best with less formatting.

在我完全转换为问题应用程序的datagridview之前,我曾经使用的是一个名为.net的电子表格工具.很好的工具,并且对格式等非常可靠... 它只是简单的作品.我换了,因为我想 使用我的应用程序的新版本可以做一些不同的事情,但是我可能应该保留原来的样子,而只是添加其他功能.如果将网格移出面板后仍未得到任何结果,我将移回电子表格设备.如果有的话 由于您无法通过网格解决该问题,因此在您的注册权益门户中注册Visual Studio Express版本和ibelve sql server express时,可以获得免费版本的工具.

What i used to use before i switched completely to the datagridview for my problem app is a tool called spreadsheetgear for .net.  Very good tool and very reliable for formatting, etc...  it just plain works.  i switched because i wanted to do some different things with a new version of my app, but i probably should have stayed how i had it and just added on the extras.  if i don't get any results after moving the grid out of the panel i will move back to spreadsheetgear.  if for some reason you are not able to get it worked out with the grid, you can get a free version of the tool when you register visual studio express editions and i beleve sql server express as well, in your registration benefits portal.


这篇关于DataGridView-如果特定值,则更改单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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