Solved

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

Posted on 2013-01-03
7
864 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

808 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