CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums




Can Reply:  No Members Can Edit: No Online: Yes
Zone: > NEWSGROUP > Asp.Net Forum > general_asp.net.faq_frequently_asked_questions Tags:
Item Type: NewsGroup Date Entered: 1/24/2006 6:55:57 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
NR
XPoints: N/A Replies: 185 Views: 237 Favorited: 0 Favorite
186 Items, 10 Pages 1 2 3 4 5 6 7 8 9 10 |< << Go >> >|
Zenuke
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource1/10/2007 2:31:16 PM

0

I am also having an isseu when attempting to use a dataset. I have the dataview set to the correct table but the dataset is not being passed fro mthe gridview onsorting command. I dont have paging so I left out the paging code.

 

GridViewSortExpression = e.SortExpression

GridView1.DataSource = SortDataSet(GridView1.DataSource) <--  this does not seem to work.

GridView1.DataBind()

Protected

Function SortDataSet(ByVal ds As System.Data.DataSet) As DataView

Dim dataView As New DataView(ds.Tables(0)) <-- getting error. "Object reference not set to an instance of an object."

If GridViewSortExpression <> String.Empty Then

dataView.Sort =

String.Format("{0} {1}", GridViewSortExpression, getsortdirection())

Return dataView

else

Return New DataView()

End If

End Function

 

Any IDeas?

 

Zenuke
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource1/10/2007 3:55:01 PM

0

well i made it work by just using a datatable except...It does not remember the sorting criteria if i do any other action (ie. select a row and save it etc)
jimatwork
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource1/12/2007 10:52:46 AM

0

Hi Ryan,

 I need to connect a datatable w/o a datasource control to a gridview, but I have one question about your example.  You are retrieving the data on every post, I thought the datatable should be created once and the gridview should handle persisting the data between posts.  What am I missing?

 

Thank you, Jim

venkatm76
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource1/17/2007 9:21:49 AM

0

Hi

I am new to ASP.net(vb)

I was trying my hand with gridview and I was successfull with indexing(paging) but with sorting I am getting the same direction for sorting.

 

What could be the reply..

 

My thanks in advance 

sarada
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource1/17/2007 10:32:35 AM

0

Hi

I will like you to put your hands in some real good way so tat you wont be having any problem with sorting and paging simultaniously

For sorting wat you need to do is pull the DataSet to DataView and sort it with <dataview>.Sort=e.SortExpression and <datagrid>.DataSource=<daatview>Idea

For paging dont use the default paging as given by .net rather put small code so tat it will work fine with sorting.first put the paging option to true. Put some good image button for next page and first page and on the click event put the following code

(for next page)

if

(<datagrid>.CurrentPageIndex<<datagrid>.PageCount-1)

{

<datagrid>.CurrentPageIndex=<datagrid>.CurrentPageIndex+1;

<datagrid>.DataSource =<dataview> ;

dg_ticket.DataBind();

lbl_page.Text=dg_ticket.CurrentPageIndex+1 +" of "+ dg_ticket.PageCount;

 

}

(for previous page)

if

(<datagrid>.CurrentPageIndex!=0)

{

<datagrid>.CurrentPageIndex=<datagrid>.CurrentPageIndex-1;

<datagrid>.DataSource = <dataview> ;

<datagrid>.DataBind();

 

}

Hope this will help u.... have happy coding.....


Sarada Prasanna Samal
venkatm76
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource1/18/2007 3:14:10 AM

0

Hi

I am okay with Paging, Problem is only with the sorting part ,

Can U give a VB.net example for the same( I could not understand <datagrid>.DataSource=<daatview>)

I tried using the AccessDatasource all worked fine with paging and sorting

  1. Now I have a Combo box which is already populated and the grid gets loaded with all coloums  with out any filter.
  2. Now when the users selects a value from the combo box and presses the button . With in the Button SUB i change the select command for the gridview and bingo all is cool the gridview has now the filtered data
  3. Now when I click the paging there comes the fun Second page is shown but with no filter

What's the solution friends

Sad 

bernhard@cedarc
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource1/24/2007 11:54:14 PM

0

I'm trying to sort a Gridview using a DataSet as above.

Dim ds As New DataSet

Dim dv As New DataView

ds = gvTableView.DataSource

dv = ds.Tables(0)

The last line generates a compile error "Error 1 Value of type 'System.Data.DataTable' cannot be converted to 'System.Data.DataView'. C:\AA_DEV\PROJECTS\WEB\AAFOS_WEB\TableView.aspx.vb 70 14 C:\...\AAFOS_WEB\'

In addition why doesn't the following work

If Session("sortDir").ToString() = "DESC" Then

gvTableView.Sort(e.SortExpression, SortDirection.Ascending)

Else

gvTableView.Sort(e.SortExpression, SortDirection.Ascending)

End If


 

GLENN
Ali.Syed
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource1/26/2007 12:04:30 AM

0

Hello there,

I'm trying to use your logic. First question, does this logic causes postback. Second I'm using the following code (vb.net), but It is doing nothing. The if condition is always false, i.e. Datatable is always null. Please help. Thanks.

Protected

Sub gvBli_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvBli.Sorting

'tbSortid.Text = e.SortExpression

'Refreshbligrid()

Dim dataTable As DataTable = gvBli.DataSource

If Not dataTable Is Nothing Then

Dim dataView As DataView = New DataView(dataTable)

dataView.Sort = e.SortExpression + " " + ConvertSortDirectionTosql(e.SortDirection)

gvBli.DataSource = dataView

gvBli.DataBind()

End If

End Sub

 

Private Function ConvertSortDirectionTosql(ByVal sortDirection As SortDirection)

Dim newSortDirection As String = ""

Select Case sortDirection

Case sortDirection.Ascending

newSortDirection =

"ASC"

Case sortDirection.Descending

newSortDirection =

"DESC"

End Select

Return newSortDirection

End Function
Ali
Aouie
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource1/26/2007 8:06:45 AM

0

what about in VB using SQL SERVER 2005, how can i do sorting / paging on GridView w/o DatasourceControl? or with Datasource control enable the sorting and paging using the EnableSortingCallback feature?

I have three Dropdownlist, everytime i sort the gridview with enableSortingCallback=true, it always sort back to the whole data? what will i going to do?

 

gogetsome
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource1/29/2007 4:07:09 PM

0

Hello, wow! this topic is popular. I need help with my code for paging and sorting. It is not working and I get this error message:

Unable to cast object of type 'System.Data.DataView' to type 'System.Data.DataTable'. 

on this line:

gvQuotes.DataSource = SortDataTable(gvQuotes.DataSource,

False)

 

I really need some help, please. What is wrong with this code?

 

'sub used to search for available quotes based on user supplied criteria

Private Sub SearchQuotes()

'set connection strig

Dim objConn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("quoteConnectionString").ToString)

'set SPROC command object

Dim objCmd As SqlCommand = New SqlCommand("BB_QH_QuoteLookup", objConn)

objCmd.CommandType = CommandType.StoredProcedure

'stored procedure parameters

Dim SearchQuoteNumber As SqlParameter = objCmd.Parameters.Add("@txtSearchQuoteNumber", SqlDbType.VarChar, 12)

SearchQuoteNumber.Value = txtSearchQuotesQuoteNumber.Text.Trim()

Dim SearchCustnmbr As SqlParameter = objCmd.Parameters.Add("@txtSearchCustnmbr", SqlDbType.Char, 15)

SearchCustnmbr.Value = txtSearchQuotesCustnmbr.Text.Trim()

Dim SearchQuotedFor As SqlParameter = objCmd.Parameters.Add("@ddlSearchQuotedFor", SqlDbType.Char, 21)

SearchQuotedFor.Value = lblSearchQFUserName.Text.Trim()

Dim SearchAreaManager As SqlParameter = objCmd.Parameters.Add("@ddlSearchAreaManager", SqlDbType.Char, 21)

SearchAreaManager.Value = lblSearchAMUserName.Text.Trim()

Dim SearchEngineer As SqlParameter = objCmd.Parameters.Add("@ddlSearchEngineer", SqlDbType.Char, 21)

SearchEngineer.Value = lblSearchENUserName.Text.Trim()

Dim SearchCreatedBy As SqlParameter = objCmd.Parameters.Add("@ddlSearchCreatedBy", SqlDbType.Char, 21)

SearchCreatedBy.Value = ddlSearchQuotesCreatedBy.SelectedValue.Trim()

'open the connection to the db

objConn.Open()

'set dataadapter and dataset and execute the sproc

Dim objDA As New SqlDataAdapter(objCmd)

Dim objDS As New DataTable("QuoteNumber")

objDA.Fill(objDS)

'fill gvQuotes with results of search

Dim dataTableRowCount As Integer = objDS.Rows.Count

If dataTableRowCount > 0 Then

gvQuotes.DataSource = objDS

gvQuotes.DataBind()

End If

'close the connection to the db

objConn.Close()

'give the user a message if the search did not return any results

If gvQuotes.Rows.Count = 0 Then

lblSearchQuotesMessage.Visible =

True

lblSearchQuotesMessage.Text =

"Your Search did not return any results."

Else

'if results are true then show the gvQuotes

gvQuotes.Visible =

True

End If

End Sub

 

Private Property GridViewSortDirection() As String

Get

Return IIf(ViewState("SortDirection") = Nothing, "ASC", ViewState("SortDirection"))

End Get

Set(ByVal value As String)

ViewState(

"SortDirection") = value

End Set

End Property

Private Property GridViewSortBLOCKED EXPRESSION As String

Get

Return IIf(ViewState("SortExpression") = Nothing, String.Empty, ViewState("SortExpression"))

End Get

Set(ByVal value As String)

ViewState(

"SortExpression") = value

End Set

End Property

Private Function GetSortDirection() As String

Select Case GridViewSortDirection

Case "ASC"

GridViewSortDirection =

"DESC"

Case "DESC"

GridViewSortDirection =

"ASC"

End Select

Return GridViewSortDirection

End Function

Protected Function SortDataTable(ByVal dataTable As DataTable, ByVal isPageIndexChanging As Boolean) As DataView

If Not dataTable Is Nothing Then

Dim dataView As New DataView(dataTable)

If GridViewSortExpression <> String.Empty Then

If isPageIndexChanging Then

dataView.Sort =

String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection)

Else

dataView.Sort =

String.Format("{0} {1}", GridViewSortExpression, GetSortDirection())

End If

End If

Return dataView

Else

Return New DataView()

End If

End Function

Protected Sub gvQuotes_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)

gvQuotes.DataSource = SortDataTable(gvQuotes.DataSource,

True)

gvQuotes.PageIndex = e.NewPageIndex

gvQuotes.DataBind()

End Sub

Protected Sub gvQuotes_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvQuotes.Sorting

GridViewSortExpression = e.SortExpression

Dim pageIndex As Integer = gvQuotes.PageIndex

gvQuotes.DataSource = SortDataTable(gvQuotes.DataSource,

False)

gvQuotes.DataBind()

gvQuotes.PageIndex = pageIndex

End Sub
Erwin@ODS
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/1/2007 12:31:30 AM

0

Can I also put my 2 cents here ?

I posted an article on how to get a GridView do automatic paging programmatically (i.e. at run time) au lieu of declaratively (at design time).

However this method uses a ObjectDataSource (but NOT in design time).

You'll find the article here : http://forums.asp.net/thread/1554769.aspx.

Please feel free to comment !

Erwin

jpa7227
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/1/2007 6:43:39 PM

0

First, thanks for the code to get started here, Ryan.  It's helped greatly.  I'm having some issues with the Paging, though.

My GridView is populated via a DataSet (actually, I set the DataSource to the first table in the DataSet).  I grab this DataSet via Remoting.  My problem is this, the GridView will be re-bound to a new DataSet based on some search criteria on the same page.  So, what I did was create a private global DataSet.  When I grab a new DataSet from the server, I assign it to the global object then re-bind that to the GridView's DataSource.  I've pretty much used all of the code you have in the Strong Coders link (sans the populate method, of course).  The one thing I do differently is substitute the global DataSet.Tables(0) in for the GridView's DataSource in the SortDataTable function.

The problem is, when I click on the link for another page in the GridView, I lose the paging footer.  It moves to the next page, but the paging information is gone.  Any ideas here?

 

dipu
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/7/2007 11:19:42 AM

0

Hi StrongTypes,

I've come across this post as I am trying to sort my grid view. It's been a year since you've made the post so I hope you can still help!

I have a data table which populates a datagrid. I understand that I need to create a dataview. I am trying to sort one of the columns in DESCENDING order. Here is my code so far:

1    Dim dt As New Data.DataTable
2    dt = ViewState("GV1")        
3    Dim dataView As New Data.DataView(dt)
4    
5    Dim s As SortDirection
6    s = SortDirection.Descending
7    dataView.Sort = String.Format("{0} {1}", e.SortExpression, s)
8    
9    GV1.DataSource = dt
10   GV1.DataBind()

 On line 7 I get an error telling me that "there is no column named price descending", where price is the column I am trying to sort in descending order.

Can you help?

Thanks

quest3297
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/14/2007 7:37:00 PM

0

hi i tried to use your code but unable to the gridview datasource it always returns me the null is there anyway to get the datasource of the grid.I cant keep the dataset in the session as it contains 1000s of records.Thank you.
MarcoLF
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource3/9/2007 1:37:07 PM

0

Hi I am using a SqlDataReader as dataSource.

Is it possible to adapt the code?

If I try to use the code on the first page I always get dataTable=null.

must i use a datatable necessarily? 

Thank you for help. 

fartboy
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource3/12/2007 3:41:00 PM

0

Hi ,

Not sure if this is the right thread for this but here we go

 I am trying to use the PageDataSource class and i do not wish to bind to a control, I want to be able to acees the data manually

i have searched far and wide and came up with this code

 

objPage.DataSource = TTable.DefaultView

 

objPage.AllowPaging =

True

objPage.PageSize = 1

objPage.CurrentPageIndex = 4

 

Dim enumerator1 As IEnumerator = Nothing

Dim dt As DataTable

Dim table2 As DataTable = DirectCast(objPage.DataSource, DataView).Table

dt = table2.Clone

enumerator1 = objPage.GetEnumerator

While enumerator1.MoveNext()

Dim obj As Object = enumerator1.Current

Dim drv As Data.DataRowView = DirectCast(obj, Data.DataRowView)

dt.ImportRow(drv.Row)

End While

 This code works but always returns the first row of data, and as can be seen i wish to get the 4th item,

Can anyone show me the error of my ways...??

 

smhouston
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource3/14/2007 4:21:56 PM

0

I've had to do it the way ChrisOngsuco said by using a custom method first. It works fine now though. Maybe it's because I only databind my gridview when the page is first loaded (when it's not postback)
infodemers
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource4/26/2007 12:36:42 PM

0

Hello Ryan,

I am using your VB.net code for sorting and paging my gridview and I can't get it done. It displays the first 25 items but when I click on sorting or paging, I get  my "EmptyDataText" No Record Found message. I know there is more data to display. My data source comes from a SQL server. I don't know what else I should specify for the moment. Here is my complete code.

I thank you very much in advance for any help!

<%

@ Page Language="VB" %>

<%

@ Import Namespace="System.Data" %>

<%

@ Import Namespace="System.Data.SqlClient" %>

<%

@ Import Namespace="System.Web.UI.WebControls" %>

<%

@ PreviousPageType VirtualPath="~/Default.aspx" %>

<

script runat="server">

Private Sub PopulateGridView1()

If Not Page.PreviousPage Is Nothing Then

Dim pp_DDL_Aclli As DropDownList

Dim pp_DDL_Region As DropDownList

Dim pp_DDL_Subregion As DropDownList

Dim pp_DDL_Zclli As DropDownList

Dim pp_DDL_Pec_code As DropDownList

Dim pp_TB_Date_In As TextBox

Dim pp_TB_Date_Out As TextBox

Dim pp_TB_Zclli As TextBox

Dim pp_TB_Mod_Number As TextBox

Dim pp_TB_Port_Number As TextBox

Dim pp_LB_Status As ListBox

Dim pp_RB_DTC As RadioButton

Dim pp_RB_SPM As RadioButton

Dim pp_RB_DTCI As RadioButton

Dim pp_RB_GWC As RadioButton

Dim pp_CB_All As CheckBox

Dim pp_CB_Pec_Code As CheckBox

pp_DDL_Aclli =

CType(PreviousPage.FindControl("DDL_Aclli"), DropDownList)

pp_DDL_Region =

CType(PreviousPage.FindControl("DDL_Region"), DropDownList)

pp_DDL_Subregion =

CType(PreviousPage.FindControl("DDL_Subregion"), DropDownList)

pp_DDL_Zclli =

CType(PreviousPage.FindControl("DDL_Zclli"), DropDownList)

pp_DDL_Pec_code =

CType(PreviousPage.FindControl("DDL_Pec_code"), DropDownList)

pp_TB_Date_In =

CType(PreviousPage.FindControl("TB_Date_In"), TextBox)

pp_TB_Date_Out =

CType(PreviousPage.FindControl("TB_Date_Out"), TextBox)

pp_TB_Zclli =

CType(PreviousPage.FindControl("TB_Zclli"), TextBox)

pp_TB_Mod_Number =

CType(PreviousPage.FindControl("TB_Mod_Number"), TextBox)

pp_TB_Port_Number =

CType(PreviousPage.FindControl("TB_Port_Number"), TextBox)

pp_LB_Status =

CType(PreviousPage.FindControl("LB_Status"), ListBox)

pp_RB_DTC =

CType(PreviousPage.FindControl("RB_DTC"), RadioButton)

pp_RB_SPM =

CType(PreviousPage.FindControl("RB_SPM"), RadioButton)

pp_RB_DTCI =

CType(PreviousPage.FindControl("RB_DTCI"), RadioButton)

pp_RB_GWC =

CType(PreviousPage.FindControl("RB_GWC"), RadioButton)

pp_CB_All =

CType(PreviousPage.FindControl("CB_All"), CheckBox)

pp_CB_Pec_Code =

CType(PreviousPage.FindControl("CB_Pec_Code"), CheckBox)

 

'Get the Value of ListBox status.

Dim Status As String = pp_LB_Status.SelectedValue

 

If (Status) = "ALL" Then

Status =

"%"

End If

 

'Get the Z_CLLI value from the TextBox Z_clli or from the DropDownList Z_clli.

Dim Zclli As String = pp_DDL_Zclli.SelectedItem.ToString

Dim TB_Zclli As String = pp_TB_Zclli.Text

 

If (pp_CB_All.Checked) = True Then

Zclli =

"%"

ElseIf Not (TB_Zclli) = "" Then

Zclli = pp_TB_Zclli.Text

End If

 

'Get the Pec_Code value.

Dim CD_PEC As String = pp_DDL_Pec_code.SelectedItem.ToString

 

If (pp_CB_Pec_Code.Checked) = True Then

CD_PEC =

"%"

End If

 

'Get the MOD type from the Radio Buttons.

Dim Mod_DTC As String

Dim Mod_SPM As String

Dim Mod_DTCI As String

Dim Mod_GWC As String

 

If pp_RB_DTC.Checked = True Then

Mod_DTC =

"DTC"

Else

Mod_DTC =

"Null"

End If

 

If pp_RB_SPM.Checked = True Then

Mod_SPM =

"SPM"

Else

Mod_SPM =

"Null"

End If

 

If pp_RB_DTCI.Checked = True Then

Mod_DTCI =

"DTCI"

Else

Mod_DTCI =

"Null"

End If

 

If pp_RB_GWC.Checked = True Then

Mod_GWC =

"GWC"

Else

Mod_GWC =

"Null"

End If

 

'Get The value from Date_In.

Dim Date_In As String = pp_TB_Date_In.Text

 

If (Date_In) = "" Then

Date_In =

"%"

End If

 

'Get The value from Date_Out

Dim Date_Out As String = pp_TB_Date_Out.Text

 

If (Date_Out) = "" Then

Date_Out =

"%"

End If

 

'Get the Value from the Module Number Texbox.

Dim Mod_Number As String = pp_TB_Mod_Number.Text

 

If (Mod_Number) = "" Then

Mod_Number =

"%"

End If

 

'Get the Value from the PI Texbox.

Dim Port_No As String = pp_TB_Port_Number.Text

 

If (Port_No) = "" Then

Port_No =

"%"

End If

 

' Dim DDL_Region As String = pp_DDL_Region.SelectedItem.ToString

' Dim DDL_Subregion As String = pp_DDL_Subregion.SelectedItem.ToString

 

'Built the query

Dim DDL_Aclli As String = pp_DDL_Aclli.SelectedItem.ToString

Dim queryString As String = "Select [tbl_Asset].* From [tbl_Asset] Where (([tbl_Asset].[A] = '" + DDL_Aclli + "') and" & _

"([tbl_Asset].[Z] like '" + Zclli + "') and ([tbl_Asset].[Status] like '" + Status + "') And (([tbl_Asset].[MOD] like " & _

"'" + Mod_DTC + "') or ([tbl_Asset].[MOD] like '" + Mod_SPM + "') or ([tbl_Asset].[MOD] like '" + Mod_DTCI + "') or ([tbl_Asset]." & _

"[MOD] like '" + Mod_GWC + "')) and ([tbl_Asset].[IN_DATE] like '" + Date_In + "') and ([tbl_Asset].[OUT_DATE] like '" + Date_Out + "" & _

"') and ([tbl_Asset].[CD_PEC] like '" + CD_PEC + "') and ([tbl_Asset].[MOD_NO] like '" + Mod_Number + "') and ([tbl_Asset].[PI] like '" + Port_No + "'));"

 

'Run the query and bind the resulting DataTable to the GridView control.

Dim dt As DataTable = GetData(queryString)

Dim dataTableRowCount As Integer = dt.Rows.Count

If dataTableRowCount > 0 Then

GridView1.DataSource = dt

GridView1.DataBind()

Else

Lable1.Text =

"There is no data for this query."

End If

End If

End Sub

 

Function GetData(ByVal queryString As String) As DataTable

'Retrieve the connection string stored in the web.config.file.

Dim connectionString As String = ConfigurationManager.ConnectionStrings("T_SwitchDBConnectionString").ConnectionString

Dim dt As New DataTable()

Try

'Connect to the database and run the query.

Dim connection As New SqlConnection(connectionString)

Dim adapter As New SqlDataAdapter(queryString, connection)

'Fill the DataTable

adapter.Fill(dt)

Catch ex As Exception

'The connection failed. Dislpay an error message.

Lable1.Text =

"Unable to connect to the database."

End Try

Return dt

End Function

 

'The following lines code are to allow Paging And Sorting.

Private Property GridViewSortDirection() As String

Get

Return IIf(ViewState("SortDirection") = Nothing, "ASC", ViewState("SortDirection"))

End Get

Set(ByVal value As String)

ViewState(

"SortDirection") = value

End Set

End Property

 

Private Property GridViewSortBLOCKED EXPRESSION As String

Get

Return IIf(ViewState("SortExpression") = Nothing, String.Empty, ViewState("SortExpression"))

End Get

Set(ByVal value As String)

ViewState(

"SortExpression") = value

End Set

End Property

Private Function GetSortDirection() As String

Select Case GridViewSortDirection

Case "ASC"

GridViewSortDirection =

"DESC"

Case "DESC"

GridViewSortDirection =

"ASC"

End Select

Return GridViewSortDirection

End Function

 

Protected Sub gridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)

GridView1.DataSource = SortDataTable(GridView1.DataSource,

True)

GridView1.PageIndex = e.NewPageIndex

GridView1.DataBind()

End Sub

 

Protected Function SortDataTable(ByVal dataTable As DataTable, ByVal isPageIndexChanging As Boolean) As DataView

If Not dataTable Is Nothing Then

Dim dataView As New DataView(dataTable)

If GridViewSortExpression <> String.Empty Then

If isPageIndexChanging Then

dataView.Sort =

String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection)

Else

dataView.Sort =

String.Format("{0} {1}", GridViewSortExpression, GetSortDirection())

End If

End If

Return dataView

Else

Return New DataView()

End If

End Function

 

Protected Sub gridView1_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)

GridViewSortExpression = e.SortExpression

Dim pageIndex As Integer = GridView1.PageIndex

GridView1.DataSource = SortDataTable(GridView1.DataSource,

False)

GridView1.DataBind()

GridView1.PageIndex = pageIndex

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

PopulateGridView1()

End Sub

 

</

script>

<!

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<

html xmlns="http://www.w3.org/1999/xhtml" >

<

head id="Head1" runat="server">

<

title>Custom_Query_Report</title>

</

head>

<

body>

<table border="0" width="100%">

<tr>

<td style="text-align:Left; width:50%; height: 23px;"><span style="font-size: 14pt; color: activecaption; text-decoration: underline">Report Results</span></td>

<td style="text-align:right; width:50%; height: 23px;"><a href="javascript:self.close('Custom_Query_Report.aspx');">Close Window</a></td>

</tr>

</table>

<form id="form1" runat="server">

<div>

<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" BorderStyle="Solid" ForeColor="#0000C0" Font-Size="Small" EmptyDataText="No records found" PagerSettings-Mode="NumericFirstLast" BorderColor="Black" BorderWidth="2px" AllowPaging="True" AllowSorting="True" OnPageIndexChanging="gridView1_PageIndexChanging" OnSorting="gridView1_Sorting" PageSize="25">

<AlternatingRowStyle BackColor="LightGray" />

<HeaderStyle BackColor="Gray" Font-Bold="True" Font-Names="Verdana" Font-Size="Smaller" />

<PagerStyle BackColor="DarkGray" Font-Names="Verdana" Font-Size="Smaller" />

<RowStyle Font-Names="Verdana" Font-Size="Smaller" />

<Columns>

<asp:BoundField Datafield="A" HeaderText="A" SortExpression="A" />

<asp:BoundField Datafield="Z" HeaderText="Z" SortExpression="Z" />

<asp:BoundField Datafield="SYS#" HeaderText="SYS#" SortExpression="SYS#" />

<asp:BoundField Datafield="TYPE" HeaderText="TYPE" SortExpression="TYPE" />

<asp:BoundField Datafield="MOD" HeaderText="MOD" SortExpression="MOD" />

<asp:BoundField Datafield="PI" HeaderText="PI" SortExpression="PI" />

<asp:BoundField Datafield="MOD_NO" HeaderText="MOD_NO" SortExpression="MOD_NO" />

<asp:BoundField Datafield="CD_PEC" HeaderText="CD_PEC" SortExpression="CD_PEC" />

<asp:BoundField Datafield="IN_DATE" HeaderText="IN_DATE" SortExpression="IN_DATE" />

<asp:BoundField Datafield="OUT_DATE" HeaderText="OUT_DATE" SortExpression="OUT_DATE" />

<asp:BoundField Datafield="COMMENTS" HeaderText="COMMENTS" SortExpression="COMMENTS" />

<asp:BoundField Datafield="STATUS" HeaderText="STATUS" SortExpression="STATUS" />

<asp:BoundField Datafield="XCON_TYPE" HeaderText="XCON_TYPE" SortExpression="XCON_TYPE" />

<asp:BoundField Datafield="MEGAROUTE" HeaderText="MEGAROUTE" SortExpression="MEGAROUTE" />

<asp:BoundField Datafield="MG_CLLI" HeaderText="MG_CLLI" SortExpression="MG_CLLI" />

<asp:BoundField Datafield="CD" HeaderText="CD" SortExpression="CD" />

<asp:BoundField Datafield="PO" HeaderText="PO" SortExpression="PO" />

</Columns>

<PagerSettings Mode="NumericFirstLast" />

</asp:GridView>

<br />

<asp:Label ID="Lable1" runat="server" ForeColor="Red"></asp:Label><br />

</div>

</form>

</

body>

</

html>
write2Yogesh_er
Asp.Net User
HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource5/7/2007 9:42:46 AM

0

hi, reyan ....

 

i m facing when i m implement this code on my application.....it gives error that unable to find  " ASC " column 

donjuanwu
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource5/20/2007 12:54:54 AM

0

Ryan,

When I click on the Page object button I see this function get called GridView_PageIndexChanging(Ojbect sender, GridViewPageEventArgs e)

{

BemsIDInfoGridView.PageIndex = e.NewPageIndex;

BemsIDInfoGridView.DataBind();

}

however, my gridview object disappear. I added this line of code to the above function

this.BemsIDInfoGridView.Visible = true;

still nothing show up when I click on the Paging object button including the GridView object. Do you have any idea why my Paging code is not working.

Thanks,

donjuanwu

186 Items, 10 Pages 1 2 3 4 5 6 7 8 9 10 |< << Go >> >|


Free Download:




   
  Privacy | Contact Us
All Times Are GMT