Solved

Removing empty columns, also changes row count with code, Why?

Posted on 2013-01-03
7
856 Views
Last Modified: 2013-01-03
I'm using below code to remove empty columns from datatable before it's databind to datagridview. When I run the sql query,  I have 14135 rows and also steping thru the code it shows 14135 for defaultview.rowfilter when I get thru below code once, but once it hits next and goes thru it the second time to check the second column, it changes the defaultview.rowfilter to 858. Why does it do that, because it only shows 858 rows on datagrid and I need it to show 14135?

Dim Col As DataColumn
For i As Integer = dTable.Columns.Count - 1 to 0 step -1
Col = dTable.Columns(i)
If col.DataType Is GetType("String") Then
    dTable.DefaultView.RowFilter = Col.ColumnName & "<>''"
    If dTable.DefaultView.Count = 0 Then
      dTable.Columns.Remove(col)
   End If
End If
Next
0
Comment
Question by:jeremyzit
  • 3
  • 2
  • 2
7 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38740697
It applies a filter to the dataview (empty column filter) and dataview then only contains rows where that column is empty. Datatable still contains all the rows. So set the datatable as the datasource not dataview.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 38740721
Or remove the filter on the defaultview AFTER running your code to show all the rows.
0
 

Author Comment

by:jeremyzit
ID: 38740809
I am setting the datatable as the datasource. How do you remove the filter on the defaultview after running the code.

        Dim Col As DataColumn
        For i As Integer = dt.Columns.Count - 1 To 0 Step -1
            Col = dt.Columns(i)
            If Col.DataType Is GetType(String) Then
                d = dt.DefaultView.Count
                dt.DefaultView.RowFilter = Col.ColumnName & "<>''"
                If dt.DefaultView.Count = 0 Then
                    dt.Columns.Remove(Col)
                End If
            End If
        Next

        dv = New DataView(dt)
        datagrid.DataSource = dt
        DataBind()
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 250 total points
ID: 38740819
dt.DefaultView.RowFilter = Nothing
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
ID: 38740828
It would be

dt.DefaultView.RowFilter = ""

And why you have

dv = New DataView(dt)

?
0
 

Author Comment

by:jeremyzit
ID: 38740855
Do you not need that on web form? "dv = New DataView(dt)" I've always put that in my code, but maybe it's only needed on windows form datagrid.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38741720
Its not needed anywhere unless you need to filter and assign the filtered data to a grid or other control.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now