Solved

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

Posted on 2013-01-03
7
877 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

617 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