473,785 Members | 2,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB.NET and sorting

(using vb.net)

Finally got paging working...not sorting taunts me...

For sorting on a datagrid - do you HAVE to use a dataview?

I used the msdn example and now get his error:

DataTable must be set prior to using DataView

So I have a dataset created from the input of the user and an SQL
statement. Then I use a dataview - well because I have to. The
dataview was made at design time along with the textbox and label.

Here is the code to generate and display my data retrieved:

Private Sub btnQuery_Click( ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles btn.Click
Dim Count As Integer
Dim ds As New DataSet
Dim SQL As String
SQL = "select [First],[Last],[Title],[Authors],[Address] from
DB1 WHERE [Title] LIKE '%" & txt.Text & "%'" & "OR [Address] LIKE '%" &
txt.Text & "%'" & " ORDER BY [Authors]"
Dim da As New SqlDataAdapter( SQL, SqlConnection2)

Try
SqlConnection2. Open()

da.Fill(ds)
Session("ds") = ds

Dim dv As DataView = ds.Tables(0).De faultView

Count = dv.Count

dg.DataSource = dv
dg.DataBind()
lblCount.Text = Count

Catch ex As Exception
txtQuery.Text = ex.Message.ToSt ring
Finally
SqlConnection2. Close()
End Try
End Sub

Private Sub dg_SortCommand( ByVal source As Object, ByVal e As
System.Web.UI.W ebControls.Data GridSortCommand EventArgs) Handles
dg.SortCommand
dv.Sort = e.SortExpressio n
dg.DataBind()
End Sub

Where do I go from here? How do I add a Datatable? I know what the
columns are from my dataset?

Thanks!

Tmuld

Nov 21 '05 #1
5 6748
For sorting on a datagrid - do you HAVE to use a dataview?
Yes.
I used the msdn example and now get his error:

DataTable must be set prior to using DataView

So I have a dataset created from the input of the user and an SQL
statement. Then I use a dataview - well because I have to. The
dataview was made at design time along with the textbox and label.

Here is the code to generate and display my data retrieved:

Private Sub btnQuery_Click( ByVal sender As System.Object, ByVal e As
System.EventAr gs) Handles btn.Click
Dim Count As Integer
Dim ds As New DataSet
Dim SQL As String
SQL = "select [First],[Last],[Title],[Authors],[Address] from
DB1 WHERE [Title] LIKE '%" & txt.Text & "%'" & "OR [Address] LIKE '%" &
txt.Text & "%'" & " ORDER BY [Authors]"
Dim da As New SqlDataAdapter( SQL, SqlConnection2)

Try
SqlConnection2. Open()

da.Fill(ds)
Session("ds") = ds

Dim dv As DataView = ds.Tables(0).De faultView

Count = dv.Count

dg.DataSource = dv
dg.DataBind()
lblCount.Text = Count

Catch ex As Exception
txtQuery.Text = ex.Message.ToSt ring
Finally
SqlConnection2. Close()
End Try
End Sub

Private Sub dg_SortCommand( ByVal source As Object, ByVal e As
System.Web.UI. WebControls.Dat aGridSortComman dEventArgs) Handles
dg.SortComma nd
dv.Sort = e.SortExpressio n
dg.DataBind()
End Sub


Try changing to this:

Private Sub dg_SortCommand( ByVal source As Object, ByVal e As
System.Web.UI.W ebControls.Data GridSortCommand EventArgs) Handles
dg.SortCommand
Dim dv As DataView = ds.Tables(0).De faultView
dv.Sort = e.SortExpressio n
dg.DataSource = dv
dg.DataBind()
End Sub

Or better yet, put your databinding logic into a separate routine, all
call that from your Button_Click, your sort event, and your paging
events.

Marcie

Nov 21 '05 #2
Wow! You must have some .NET experience!

My dataset was created programatically and dimmed in the button routine
- so when I get to the sort routine - I get a squiggly stating
ds.Tables(0).De fautlview is not declared.

So if I put it in a separate routine from the btnClick - it will
alleviate this?

Or can I pass it using session variable - or is that really bad coding?

Private Sub dg_SortCommand( ByVal source As Object, ByVal e As
System.Web.UI.W ebControls.Data GridSortCommand EventArgs) Handles
dg.SortCommand

Dim ds as new dataset
ds=session("ds" )

Dim dv As DataView = ds.Tables(0).De faultView
dv.Sort = e.SortExpressio n
dg.DataSource = dv
dg.DataBind()
End Sub

Many thanks for the help and suggestions!

Tmuld.

Nov 21 '05 #3
Put that all in a separate routine, creating/dimming the DataSet and
setting the grid's DataSource.

Hope that helps!
Marcie

On 20 Apr 2005 07:52:31 -0700, "Tmuld" <tm******@splic ed.com> wrote:
Wow! You must have some .NET experience!

My dataset was created programatically and dimmed in the button routine
- so when I get to the sort routine - I get a squiggly stating
ds.Tables(0).D efautlview is not declared.

So if I put it in a separate routine from the btnClick - it will
alleviate this?

Or can I pass it using session variable - or is that really bad coding?

Private Sub dg_SortCommand( ByVal source As Object, ByVal e As
System.Web.UI. WebControls.Dat aGridSortComman dEventArgs) Handles
dg.SortComma nd

Dim ds as new dataset
ds=session("ds" )

Dim dv As DataView = ds.Tables(0).De faultView
dv.Sort = e.SortExpressio n
dg.DataSource = dv
dg.DataBind()
End Sub

Many thanks for the help and suggestions!

Tmuld.


Nov 21 '05 #4


I guess that would make sense, but I am not sure where to put it.

Right now, poorly coded use of session variables to pass data between
routines works. But not the most optimized.

My dataset filling is dependent on the SQL statement that comes from
the user entered data.

Are you saying put this :

Private sub MakeDs()

Dim ds As New DataSet
Dim SQL As String
SQL = "select [First],[Last],[Title],[Authors],[Address] from
DB1 WHERE [Title] LIKE '%" & txt.Text & "%'" & "OR [Address] LIKE '%" &
txt.Text & "%'" & " ORDER BY [Authors]"
Dim da As New SqlDataAdapter( SQL, SqlConnection2)

da.Fill(ds)
Session("ds") = ds

Dim dv As DataView = ds.Tables(0).De faultView

End Sub

in a routine?

I guess the the problem I am having is do variables in subroutines stay
declared outside routines?

Thanks,

Tmuld.

Nov 21 '05 #5
That's fine, but also add:

dg.DataSource = dv
dg.DataBind()

Marcie

On 20 Apr 2005 11:11:24 -0700, "Tmuld" <tm******@splic ed.com> wrote:


I guess that would make sense, but I am not sure where to put it.

Right now, poorly coded use of session variables to pass data between
routines works. But not the most optimized.

My dataset filling is dependent on the SQL statement that comes from
the user entered data.

Are you saying put this :

Private sub MakeDs()

Dim ds As New DataSet
Dim SQL As String
SQL = "select [First],[Last],[Title],[Authors],[Address] from
DB1 WHERE [Title] LIKE '%" & txt.Text & "%'" & "OR [Address] LIKE '%" &
txt.Text & "%'" & " ORDER BY [Authors]"
Dim da As New SqlDataAdapter( SQL, SqlConnection2)

da.Fill(ds)
Session("ds") = ds

Dim dv As DataView = ds.Tables(0).De faultView

End Sub

in a routine?

I guess the the problem I am having is do variables in subroutines stay
declared outside routines?

Thanks,

Tmuld.


Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2533
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys() sorted_feature_vector.sort() #feature_vector.keys()=sorted_feature_vector
0
2736
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update not the whole page, but a portion of the page. Strangely enough the URL below http://beta.asp.net/QUICKSTARTV20/aspnet/doc/ctrlref/data/gridview.aspx (VB GridView Paging and Sorting Callbacks example)
7
3266
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) CType(local4, Short) = CType(src, Short)
19
25467
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to sort columns based on the column header the user has clicked in both Ascending and Descending formats.
10
2796
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to write such layers myself I got stuck on how to get filtered or sorted data from the data-layer. This is what I got: Objects
4
3101
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in grid. And with rebinding, sorting image (little triangle on the column header) goes away. I need to show sorting image as well custom sorting. Please help. Thanks
7
4827
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the expandable row, the hidden row is made visible with css. The problem is when i sort the rows, the hidden rows get sorted as well which i don't want and want to be moved (while sorting) relative to their parent rows. The following is my complete html code...
1
7192
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar methods or syntax to a less experienced perl coder. I will post links to online resources you can read if necessary. Experienced perl coders might find nothing new or useful contained in this article. Short Review In part one I showed you some...
5
3187
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The search algorithms that can be used are Linear Search and Binary Search. The sorting algorithms are bubble, selection and Insertion sort. First, the user is asked whether he/she wants to perform a search option, a sort operation, or exit the program. If...
5
4957
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link: http://www.jaredmoore.com/tablesorter/docs/salestable.html Here is some jquery js that I think...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.