如何在datagridview中删除特定数据 [英] How do I strike out a particular data in datagridview

查看:359
本文介绍了如何在datagridview中删除特定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一个datagridview,如果数据中的一个单元格显示为Void,我希望查看特定数据,但是当我使用代码时,我使用datagridview中的所有数据,这意味着接受第一个尽管所述数据中的一些数据显示为活动,但所有数据的参数都是活跃的



我尝试了什么:



i have a datagridview in my project, i wish to strike a particular data if one of the cell in the data reads "Void" but when i did with the code i used all data in the datagridview was strike meaning accepting the first argument to all data despite some of the data in the said cells reads "Active"

What I have tried:

For Each r As DataGridViewRow In frmCheckOut_Room.DataGridView2.Rows
                        If (r.Cells(9).Value) = "Void" Then
                            r.DefaultCellStyle.ForeColor = Color.Red
                            r.DefaultCellStyle.Font = New Font("Microsoft Sans Serif", 8, FontStyle.Strikeout)
                        ElseIf (r.Cells(9).Value) = "Active" Then
                            r.DefaultCellStyle.Font = New Font("Microsoft Sans Serif", 8)
                            r.DefaultCellStyle.BackColor = Color.Orange
                        End If
                    Next

推荐答案

设置单元格的样式,而不是默认的单元格样式:

Set the styles for the cell, rather than the default cell style:
For Each r As DataGridViewRow In frmCheckOut_Room.DataGridView2.Rows
    Dim cell As DataGridViewCell = r.Cells(9)
    If cell.Value = "Void" Then
        cell.Style.ForeColor = Color.Red
        cell.Font = New Font("Microsoft Sans Serif", 8, FontStyle.Strikeout)
    ElseIf cell.Value = "Active" Then
        cell.Style.Font = New Font("Microsoft Sans Serif", 8)
        cell.Style.BackColor = Color.Orange
    End If
Next



注意:由于样式是指一次性对象,因此将样式存储在字段中并重用相同的样式实例可能是个好主意。



Windows窗体DataGridView控件中的单元格样式Microsoft Docs [ ^ ]


NB: Since the style refers to disposable objects, it would probably be a good idea to store the styles in fields and reuse the same style instance.

Cell Styles in the Windows Forms DataGridView Control | Microsoft Docs[^]


这篇关于如何在datagridview中删除特定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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