如何隐藏vb.net中包含空值的datagridview行 [英] How to hide datagridview rows that contain null values in vb.net

查看:221
本文介绍了如何隐藏vb.net中包含空值的datagridview行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我希望找到一种方法来隐藏我的datagridview的某些行,如果它们不包含任何内容或null。我的datagridview绑定到一个excel工作表的数据源。我希望在导入时可以隐藏空行并仅显示包含值的行。我觉得这样的东西会起作用,但我最近没有运气。

  Dim 作为 布尔 =  True  

对于 i 作为 整数 = 0 dataGridView1.Rows.Count - 1
空= True
对于 j 作为 整数 = 0 dataGridView1.Columns.Count - 1
如果 dataGridView1.Rows(i).Cells(j).Value IsNot Nothing AndAlso dataGridView1 .Rows(i).Cells(j).Value.ToString()<> 然后
清空= 错误
退出 对于
结束 如果
下一步
如果那么
dataGridView1.Rows.RemoveAt(i)
结束 如果
下一步















如果有人能就此事提出任何建议或帮助,我将不胜感激!

解决方案

抱歉我给出了另一个答案:我最近专注于网络编程,我创建了一个错误控制的解决方案。



我在WinForms中使用数据绑定网格的原因很少,但我相信这会做你想要的:

 私有  Sub  TestGridView_DataBindingComplete( ByVal 发​​件人作为 对象,_ 
ByVal e As DataGridViewBindingCompleteEventArgs)_
Handles DataGridView1。 DataBindingComplete

对于 每个正如 DataGridViewRow CType (发件人,DataGridView)。行
Dim 可见 As Boolean = True

' 执行此操作以检查行中的所有单元格
对于 i 正如 整数 = 0 Row.Cells.Count - 1
如果 Row.Cells(i).Value 没什么 然后
可见= 错误
退出 对于
结束 如果
下一步

' 或者您可以检查特定列的值
如果行。单元格( 0 )。值 OrElse _
(IsNumeric(Row.Cells( 0 )。值) AndAlso CInt (Row.Cells( 0 )。值) < 0 然后
Visible = False
结束 如果

Row.Visible = Visible
下一步
结束 Sub

绑定了所有数据后触发 DataBindingComplete 事件。逐行检查并确定该行是否可见。



代码的第一部分检查行中的每个单元格值,以确定该行是否应该可见。



第二个显示如何针对特定值测试特定列;在这种情况下,只查看第一列,如果没有任何内容,则禁止该行,否则为负值的整数。 OrElse AndAlso 加入者使评估过程短路:如果值 Nothing then语句为true,数字测试永远不会为单元格完成;如果该值不是数字,那么它将不会尝试转换它。



设置(或不设置)的值后可见,行可见性已设置,我们继续前进到下一行,直到每行都经过测试。这是一个非常缓慢的方法,如果你有很多行会有明显的滞后,但我没有看到任何其他方式。



如果这符合您的需要,请告诉我。如果是这样,请不要忘记接受解决方案:)


我从未尝试过这样做,但我认为这符合您的需求:

< pre lang =vb> 受保护的 Sub TestGrid_RowDataBound( ByVal 发​​件人作为 对象,_
ByVal e As GridViewRowEventArgs)
Dim DVR As DataRowView = CType (e.Row.DataItem,DataRowView)

如果 e.Row.RowType = DataControlRowType.DataRow 那么
Dim 可见作为 布尔 = True
对于 i 作为 整数 = 0 e.Row.Cells.Count - 1
如果 DVR(i) 什么 然后
可见= 错误
退出 对于
结束 如果
下一步
e.Row.Visible =可见
结束 如果
结束 Sub



在网格的 RowDat中aBound 事件,首先确定您是否绑定数据行;我们不需要在标题行,页脚行等上运行此代码。



接下来,获取对 DataItem的引用,对于该行,将其转换为 DataRowView 。此对象具有默认属性,您可以通过索引或列名引用列。



执行检查:您可以使用整数循环或仅选择列来测试每一列。该属性返回 Object ,因此您可能希望将其强制转换为特定的数据类型以查找特定值(如果整数列较少,可能要禁止该行例如,比零。如果你使用SQL中的数据源,你将测试 DBNull.Value 来查找SQL null。



如果行符合禁止此行的规范,则只需将行设置为不可见。当控件被渲染时,输出中将省略任何不可见的行。



更新我将示例代码更改为更符合满足您的要求。


删除特定值



  Dim  rdval 作为 整数 = Form1.DGVlap.RowCount  -  < span class =code-digit> 1  
如果 rdval> 0 然后
尝试
对于 i 作为 整数 = 0 rdval
如果我> 0 然后
如果 Form1 .DGVlap.Rows(i).Cells( 12 )。Value.ToString = 1 然后
Form1.DGVlap.Rows(i).Visible = 错误
结束 如果
结束 如果
下一步
Catch ex As 异常
MsgBox(ex.Message)
结束 尝试
结束 如果





如果值= 1,则在第0行获取错误,我使用add删除第0行 - 如果i> 0然后 -


Hi I'm hoping to find a way to hide certain rows of my datagridview if they contain nothing or null. My datagridview is bound to a datasource that is an excel worksheet. I'm hoping on import that I can hide the rows that are empty and display only those that contain values. I'm thiking something like this will work but I've had no luck as of late.

Dim Empty As Boolean = True

For i As Integer = 0 To dataGridView1.Rows.Count - 1
    Empty = True
    For j As Integer = 0 To dataGridView1.Columns.Count - 1
        If dataGridView1.Rows(i).Cells(j).Value IsNot Nothing AndAlso dataGridView1.Rows(i).Cells(j).Value.ToString() <> "" Then
            Empty = False
            Exit For
        End If
    Next
    If Empty Then
        dataGridView1.Rows.RemoveAt(i)
    End If
Next








If anyone can give any suggestions or help on the matter I would greatly appreciate it!

解决方案

Sorry about the other answer I gave: I have been so focused on web programming recently that I created a solution for the wrong control.

I have had very little cause to use databound grids in WinForms, but I believe this will do what you want:

Private Sub TestGridView_DataBindingComplete(ByVal sender As Object, _
ByVal e As DataGridViewBindingCompleteEventArgs) _
Handles DataGridView1.DataBindingComplete

    For Each Row As DataGridViewRow In CType(sender, DataGridView).Rows
        Dim Visible As Boolean = True

        'Do this to inspect all cells in the row
        For i As Integer = 0 To Row.Cells.Count - 1
            If Row.Cells(i).Value Is Nothing Then
                Visible = False
                Exit For
            End If
        Next

        'Or you can check specific columns for their values
        If Row.Cells(0).Value Is Nothing OrElse _
        (IsNumeric(Row.Cells(0).Value) AndAlso CInt(Row.Cells(0).Value) < 0) Then
            Visible = False
        End If

        Row.Visible = Visible
    Next
End Sub

The DataBindingComplete event is fired after all of the data has been bound. Go through row by row and make a determination on whether or not the row should be visible or not.

The first section of code inspects every cell value in the row to determine whether or not the row should be visible.

The second one shows how to test specific columns for specific values; in this case, look only at the first column and suppress the row if it either nothing or else an integer with a negative value. The OrElse and AndAlso joiners short-circuit the evaluation process: if the value is Nothing then statement is true and the numeric tests never get done for the cell; if the value is not numeric then it will not try to convert it.

After setting (or not) the value of Visible, the row visibility is set and we move on to the next row, until every row has been tested. This is a very slow way of doing this, and there will be a noticeable lag if you have a lot of rows, but I don't see any other way.

Let me know if this does what you need. If so, don't forget to accept the solution :)


I've never tried doing this, but I think this will suit your needs:

Protected Sub TestGrid_RowDataBound(ByVal sender As Object, _
ByVal e As GridViewRowEventArgs)
    Dim DVR As DataRowView = CType(e.Row.DataItem, DataRowView)

    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim Visible As Boolean = True
        For i As Integer = 0 To e.Row.Cells.Count - 1
            If DVR(i) Is Nothing Then
                Visible = False
                Exit For
            End If
        Next
        e.Row.Visible = Visible
    End If
End Sub


In the grid's RowDataBound event, first determine if you are binding a data row; we do not need to run this code on header rows, footer rows, etc.

Next, get a reference to the DataItem, for the row, casting it to DataRowView. This object has a default property, and you can reference the columns either by index or by column name.

Perform your checks: you can test every column using an integer loop, or just selected columns. The property returns an Object, so you may want to cast it to a specific data type to look for particular values (maybe you want to suppress the row if an integer column is less than zero, for example). If you use this with a data source from SQL, you would test against DBNull.Value to look for a SQL null.

If the row meets the specifications for "suppress this row", then simply set the row to be not visible. When the control gets rendered, any invisible rows will be omitted from the output.

Update I changed the sample code to be more in line with what you are asking for.


To remove spesific value

Dim rdval As Integer = Form1.DGVlap.RowCount - 1
    If rdval > 0 Then
       Try
            For i As Integer = 0 To rdval
               If i > 0 Then
                  If Form1.DGVlap.Rows(i).Cells(12).Value.ToString = "1" Then
                     Form1.DGVlap.Rows(i).Visible = False
                  End If
               End If
            Next
       Catch ex As Exception
            MsgBox(ex.Message)
       End Try
     End If



Get Error on Row 0 if value = 1, me remove row 0 with add - If i > 0 Then -


这篇关于如何隐藏vb.net中包含空值的datagridview行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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