PC Review


Reply
Thread Tools Rate Thread

VB.NET and sorting

 
 
Tmuld
Guest
Posts: n/a
 
      19th Apr 2005
(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.EventArgs) 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).DefaultView

Count = dv.Count

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

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

Private Sub dg_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
dg.SortCommand
dv.Sort = e.SortExpression
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

 
Reply With Quote
 
 
 
 
Marcie Jones
Guest
Posts: n/a
 
      19th Apr 2005

>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.EventArgs) 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).DefaultView
>
> Count = dv.Count
>
> dg.DataSource = dv
> dg.DataBind()
> lblCount.Text = Count
>
> Catch ex As Exception
> txtQuery.Text = ex.Message.ToString
> Finally
> SqlConnection2.Close()
> End Try
> End Sub
>
>Private Sub dg_SortCommand(ByVal source As Object, ByVal e As
>System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
>dg.SortCommand
> dv.Sort = e.SortExpression
> dg.DataBind()
> End Sub


Try changing to this:

Private Sub dg_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
dg.SortCommand
Dim dv As DataView = ds.Tables(0).DefaultView
dv.Sort = e.SortExpression
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

 
Reply With Quote
 
 
 
 
Tmuld
Guest
Posts: n/a
 
      20th Apr 2005
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).Defautlview 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.DataGridSortCommandEventArgs) Handles
dg.SortCommand

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

Dim dv As DataView = ds.Tables(0).DefaultView
dv.Sort = e.SortExpression
dg.DataSource = dv
dg.DataBind()
End Sub

Many thanks for the help and suggestions!

Tmuld.

 
Reply With Quote
 
Marcie Jones
Guest
Posts: n/a
 
      20th Apr 2005
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" <(E-Mail Removed)> 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).Defautlview 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.DataGridSortCommandEventArgs) Handles
>dg.SortCommand
>
> Dim ds as new dataset
> ds=session("ds")
>
> Dim dv As DataView = ds.Tables(0).DefaultView
> dv.Sort = e.SortExpression
> dg.DataSource = dv
> dg.DataBind()
> End Sub
>
>Many thanks for the help and suggestions!
>
>Tmuld.


 
Reply With Quote
 
Tmuld
Guest
Posts: n/a
 
      20th Apr 2005


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).DefaultView

End Sub

in a routine?

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

Thanks,

Tmuld.

 
Reply With Quote
 
Marcie Jones
Guest
Posts: n/a
 
      20th Apr 2005
That's fine, but also add:

dg.DataSource = dv
dg.DataBind()

Marcie

On 20 Apr 2005 11:11:24 -0700, "Tmuld" <(E-Mail Removed)> 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).DefaultView
>
>End Sub
>
>in a routine?
>
>I guess the the problem I am having is do variables in subroutines stay
>declared outside routines?
>
>Thanks,
>
>Tmuld.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic sorting (giving max and min) based on custom sorting lis Joe Lewis Microsoft Excel Worksheet Functions 4 23rd Nov 2008 06:12 AM
Sorting / Grouping and sorting in the stored procedure RJ Microsoft Access ADP SQL Server 3 6th Mar 2008 01:41 AM
Sorting Sorting Sorting =?Utf-8?B?U2t5ZGl2ZXI=?= Microsoft Excel Misc 4 3rd Jun 2006 02:42 PM
Program Menu Sorting... and UN sorting Overlord Windows XP Customization 7 30th Mar 2005 06:59 PM
Sorting ListBox results or transposing ListBox values to other cells for sorting Rob Microsoft Excel Programming 1 9th Jul 2003 04:35 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:34 PM.