从数据表中删除空行 [英] Remove empty rows from datatable

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

问题描述

嗨大家好,我有问题我想在将数据绑定到我的datatagridview之前删除我的数据表中的空行。我从sql表中获取源代码。



这就是我所拥有的:

Hi Guys, I have problem I want to remove empty rows in my datatable that before binding it to my datatagridview. I get the source from a sql table.

this is what I have:

If String.IsNullOrEmpty(dt.Rows(i)(col.HeaderText).ToString()) = False Then
            isEmpty = False
            'Exit For
          ElseIf Not String.IsNullOrEmpty(dt.Rows(i)(col.HeaderText).ToString()) = False Then
            isEmpty = True
            If isEmpty = True Then
              dt.Rows.RemoveAt(i)
              i -= 1
            End If
          End If





如果某个列为空,则此代码将被删除,如果整行都为空,我想要删除。



任何帮助将不胜感激



this code removes if a certain column is is empty, I want if the entire row is empty to be removed.

Any help would be appreciated

推荐答案

而不是删除行,它(如digimanus所说)最好不要检索空行开始。

但是,肯定只是修改代码以循环遍历所有行并检查为空,而不是使用当前的 if



这会给你带来什么问题?
Rather than deleting rows, it would (as digimanus says) be better to not retrieve empty rows to start with.
However, surely it is just a case of modifying your code to loop through all rows and check for empty, rather than using the simple if that you currently have?

What part of this is giving you problems?


请检查这段代码:



Please check this piece of code:

'Dim dt As New DataTable

Dim valuesarr As String = String.Empty
For i As Integer = 0 To dt.Rows.Count - 1
    Dim lst As New List(Of Object)(dt.Rows(i).ItemArray)
    For Each s As Object In lst
        valuesarr &= s.ToString
    Next
    If String.IsNullOrEmpty(valuesarr) Then
        'Remove row here, this row do not have any value
    End If
Next





此代码将从数据表中删除所有完全空白的行。



This code will remove all completely blank rows from a datatable.


for(int h = 0 ; h< Ds.Tables [0] .Rows.Count; h ++)

{

if(Ds.Tables [0] .Rows [h] .IsNull(0)== true)

{

Ds.Tables [0] .Rows [h]。删除();

}

}
for (int h = 0; h < Ds.Tables[0].Rows.Count; h++)
{
if(Ds.Tables[0].Rows[h].IsNull(0)==true)
{
Ds.Tables[0].Rows[h].Delete();
}
}


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

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