jeremyzit
asked on
Removing empty columns, also changes row count with code, Why?
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.RowFilt er = Col.ColumnName & "<>''"
If dTable.DefaultView.Count = 0 Then
dTable.Columns.Remove(col)
End If
End If
Next
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.RowFilt
If dTable.DefaultView.Count = 0 Then
dTable.Columns.Remove(col)
End If
End If
Next
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.
Or remove the filter on the defaultview AFTER running your code to show all the rows.
ASKER
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()
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()
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
Its not needed anywhere unless you need to filter and assign the filtered data to a grid or other control.