• 0

[.NET 3.5]Filter columns on a Grid


Question

Hey All,

I've been searching high and low for a way to 'emulate' excel filtering in a grid view in the .NET app I am writing.. the reason being that on occasion very large datasets are being bound to my existing datagridview which is a complete pain to work with.. (however users need to have access to them all..)

The closest thing I have found (and what I'm currently working with) is this from MS: http://msdn.microsoft.com/en-us/library/aa480727.aspx

It does pretty much exactly what I'm after with the exception of multi-select (see attached)

Anyone have any suggestions?

Thanks!

post-24841-1243166016.png

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

so your question is: How do I sort on multiple columns within ASP.NET? Like this:

Sub SortGrid()
		If Not Session("sortarray") Is Nothing Then
			Dim expression As String = ""
			Dim dt As New DataTable
			Dim i As Integer
			Dim sortArray() As String
			sortArray = Split(Session("sortArray"), ",")
			For i = 0 To sortArray.Length - 1
				If i > 0 Then
					expression = expression & "," & dgSearchResults.Columns(sortArray(i)).SortExpression.ToString
				Else
					expression = dgSearchResults.Columns(sortArray(i)).SortExpression.ToString
				End If
			Next
			sortArray = Nothing
			If Session("SortDirection") Is Nothing Then Session("SortDirection") = "ASC"
			If Len(Trim(expression)) > 0 Then
				If Not Session("dt") Is Nothing Then dt = Session("dt")
				If Session("unique") = True Then dt = Session("filtered")
				Dim dv = dt.DefaultView
				dgSearchResults.PageIndex = 0
				dv.Sort = expression & " " & Session("SortDirection")
				dgSearchResults.DataSource = dv
				ShowHideColumns()
				dgSearchResults.Width = "879"
				dgSearchResults.DataBind()
				dv = Nothing
				dt = Nothing
				expression = Nothing
			End If
		End If
	End Sub

Or, did I misunderstand the question?

If you want to know how you get the columns the user wants, use an available listbox and a selected listbox with multiple selections and create two buttons like "-->" and "<--" then loop thru the selected to get your expression.

Link to comment
Share on other sites

  • 0
Or, did I misunderstand the question?

lol, yes totally :)

sorry, I thought my question with nice screen shot was pretty expletive.. I'll try again, although sorting on multiple columns is quite handy.. so I'll save that code for a later date :)

...

Ok, so.. I don't want to sort my columns I want to filter my rows by what is in the columns.

In excel you can filter on x items in that column which will act in the same way as a where clause would if you were doing Select * from Table

is there a control that anyone knows of that can emulate this.. to do the "where clause" in the UI, not on the query.. Preferably free, but I am not adverse to paying for it if necessary

ask if there are any more questions :)

post-24841-1243186921.png

Link to comment
Share on other sites

  • 0

Can't you write something yourself that gets all distinct values from a column (so you can display a list of filters), then apply a RowFilter where clause to the datatable?

Link to comment
Share on other sites

  • 0
Can't you write something yourself that gets all distinct values from a column (so you can display a list of filters), then apply a RowFilter where clause to the datatable?

+1

Or use LINQ to filter.

Link to comment
Share on other sites

  • 0
+1

Or use LINQ to filter.

First off, LINQ is a no goer because I have deliberately built this application to not know what the underlying table structure is, therefore datasets are untyped, making LINQ a bit pointless..

Can't you write something yourself that gets all distinct values from a column (so you can display a list of filters), then apply a RowFilter where clause to the datatable?

The issue I have with getting all the distinct values and writing my own filters is that I don't know:

a) how many columns there are going to be, and

b) what any of the column names will be

Because my DataGridView is purposely generic I think it would be a complete nightmare, and a bit of a mess, to dynamically create listboxes for each field returned, although that thought had crossed my mind..

I'm going to have a look at extending the DataGridView control tomorrow, to see if I can get it to do what I want.. Never looked at trying that before so failing that I think I'll just fall back to this: http://msdn.microsoft.com/en-us/library/aa480727.aspx

Thanks

Link to comment
Share on other sites

  • 0

Sorry about my confusion. The only filtering I've done is where I end up with, let's say, a document related to several countries - the grid typically will show the same doc several times (one for each country).

In this case I clone the search results (something like session("Filtered") = datatable.clone()). I then loop thru it and remove all the duplicate docs. This gives me two datasets so the user can then toggle unique on/off and I just flip between them without any more SQL work.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.