Solved

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

Posted on 2013-01-03
7
867 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
Regex validation 2 28
VB.Net - String Manipulation Scenario 2 26
Vb.net threads keep increasing 2 38
Groupbox Control ? 2 20
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

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