I have a gridview thats get some data from a db, all is working, but when i hit the HeaderText to do some sorting, its not working then i get this error
GridView 'MySource' triggeredactionSorting
which was nothandled.
Imports System.IO
Imports System.Data
Imports System.Data.OleDb
Partial Class _default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Code using DataSet
Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MyConnStr").ConnectionString)
Dim sql As String = "select * from email_list"
Using adapter As OleDbDataAdapter = New OleDbDataAdapter(sql, connection)
Dim ds As New DataSet()
adapter.Fill(ds)
MySource.DataSource = ds
MySource.DataBind()
End Using
End Using
End Sub
Sub MySource_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Header Then
Dim sortColumnIndex As Integer = GetSortColumnIndex()
If sortColumnIndex <> -1 Then
AddSortImage(sortColumnIndex, e.Row)
End If
End If
End Sub
' This is a helper method used to determine the index of the
' column being sorted. If no column is being sorted, -1 is returned.
Function GetSortColumnIndex() As Integer
Dim field As DataControlField
For Each field In MySource.Columns
If field.SortExpression = MySource.SortExpression Then
Return MySource.Columns.IndexOf(field)
End If
Next
Return -1
End Function
' This is a helper method used to add a sort direction
' image to the header of the column being sorted.
Sub AddSortImage(ByVal columnIndex As Integer, ByVal row As GridViewRow)
Dim sortImage As New Image()
If MySource.SortDirection = SortDirection.Ascending Then
sortImage.ImageUrl = "~/images/sort_asc_arrow.gif"
sortImage.AlternateText = "Ascending Order"
Else
sortImage.ImageUrl = "~/images/sort_desc_arrow.gif"
sortImage.AlternateText = "Descending Order"
End If
row.Cells(columnIndex).Controls.Add(sortImage)
End Sub
End Class
sry im new to this, but what part of this code will u place in that Protected Sub !?
Sub MySource_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Header Then
Dim sortColumnIndex As Integer = GetSortColumnIndex()
If sortColumnIndex <> -1 Then
AddSortImage(sortColumnIndex, e.Row)
End If
End If
End Sub
' This is a helper method used to determine the index of the
' column being sorted. If no column is being sorted, -1 is returned.
Function GetSortColumnIndex() As Integer
Dim field As DataControlField
For Each field In MySource.Columns
If field.SortExpression = MySource.SortExpression Then
Return MySource.Columns.IndexOf(field)
End If
Next
Return -1
End Function
' This is a helper method used to add a sort direction
' image to the header of the column being sorted.
Sub AddSortImage(ByVal columnIndex As Integer, ByVal row As GridViewRow)
Dim sortImage As New Image()
If MySource.SortDirection = SortDirection.Ascending Then
sortImage.ImageUrl = "~/images/sort_asc_arrow.gif"
sortImage.AlternateText = "Ascending Order"
Else
sortImage.ImageUrl = "~/images/sort_desc_arrow.gif"
sortImage.AlternateText = "Descending Order"
End If
row.Cells(columnIndex).Controls.Add(sortImage)
End Sub
I suggest the code that is in Page_Load now. I also suggest you do that code in Page_Load only when IsPostBack is False.
The code to go in the Sorting handler will need to add "order by' and the field to sort on to the query.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
Marked as answer by siraero on Feb 27, 2012 08:50 PM
siraero
Member
419 Points
604 Posts
gridview sorting is not working
Feb 24, 2012 09:28 PM|LINK
Hi
I have a gridview thats get some data from a db, all is working, but when i hit the HeaderText to do some sorting, its not working then i get this error
GridView 'MySource' triggered action Sorting which was not handled.
My code is:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="default.aspx.vb" Inherits="_default" %> <!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 runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="MySource" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" DataKeyNames="email_id" AllowPaging="True" emptydatatext="Ingen Data tilgængelig." allowsorting="true" EnableViewState="False" onrowcreated="MySource_RowCreated"> <AlternatingRowStyle BackColor="White" /> <EditRowStyle BackColor="#2461BF" /> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#EFF3FB" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#F5F7FB" /> <SortedAscendingHeaderStyle BackColor="#6D95E1" /> <SortedDescendingCellStyle BackColor="#E9EBEF" /> <SortedDescendingHeaderStyle BackColor="#4870BE" /> <Columns> <asp:TemplateField HeaderText="Vælg"> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="email_name" HeaderText="Navn" SortExpression="email_name" /> <asp:BoundField DataField="email_gen" HeaderText="Køn" SortExpression="email_gen" /> <asp:BoundField DataField="email_old" DataFormatString="{0:d}" HeaderText="Alder" SortExpression="email_gen" /> <asp:BoundField DataField="email_adr" HeaderText="Email" /> </Columns> </asp:GridView> </div> </form> </body> </html>Imports System.IO Imports System.Data Imports System.Data.OleDb Partial Class _default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' Code using DataSet Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MyConnStr").ConnectionString) Dim sql As String = "select * from email_list" Using adapter As OleDbDataAdapter = New OleDbDataAdapter(sql, connection) Dim ds As New DataSet() adapter.Fill(ds) MySource.DataSource = ds MySource.DataBind() End Using End Using End Sub Sub MySource_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.Header Then Dim sortColumnIndex As Integer = GetSortColumnIndex() If sortColumnIndex <> -1 Then AddSortImage(sortColumnIndex, e.Row) End If End If End Sub ' This is a helper method used to determine the index of the ' column being sorted. If no column is being sorted, -1 is returned. Function GetSortColumnIndex() As Integer Dim field As DataControlField For Each field In MySource.Columns If field.SortExpression = MySource.SortExpression Then Return MySource.Columns.IndexOf(field) End If Next Return -1 End Function ' This is a helper method used to add a sort direction ' image to the header of the column being sorted. Sub AddSortImage(ByVal columnIndex As Integer, ByVal row As GridViewRow) Dim sortImage As New Image() If MySource.SortDirection = SortDirection.Ascending Then sortImage.ImageUrl = "~/images/sort_asc_arrow.gif" sortImage.AlternateText = "Ascending Order" Else sortImage.ImageUrl = "~/images/sort_desc_arrow.gif" sortImage.AlternateText = "Descending Order" End If row.Cells(columnIndex).Controls.Add(sortImage) End Sub End Classlakhi_moni3
Member
34 Points
12 Posts
Re: gridview sorting is not working
Feb 24, 2012 10:01 PM|LINK
Add the OnSorting event to the gridview in the aspx page
OnSorting="MySource_Sorting
In the codebehind require to include the corresponding method
protected void MySource_Sorting(object sender, GridViewSortEventArgs e)
{
//enter the sorting code here
}
siraero
Member
419 Points
604 Posts
Re: gridview sorting is not working
Feb 25, 2012 08:24 AM|LINK
sry im new to this, but what part of this code will u place in that Protected Sub !?
Sub MySource_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.Header Then Dim sortColumnIndex As Integer = GetSortColumnIndex() If sortColumnIndex <> -1 Then AddSortImage(sortColumnIndex, e.Row) End If End If End Sub ' This is a helper method used to determine the index of the ' column being sorted. If no column is being sorted, -1 is returned. Function GetSortColumnIndex() As Integer Dim field As DataControlField For Each field In MySource.Columns If field.SortExpression = MySource.SortExpression Then Return MySource.Columns.IndexOf(field) End If Next Return -1 End Function ' This is a helper method used to add a sort direction ' image to the header of the column being sorted. Sub AddSortImage(ByVal columnIndex As Integer, ByVal row As GridViewRow) Dim sortImage As New Image() If MySource.SortDirection = SortDirection.Ascending Then sortImage.ImageUrl = "~/images/sort_asc_arrow.gif" sortImage.AlternateText = "Ascending Order" Else sortImage.ImageUrl = "~/images/sort_desc_arrow.gif" sortImage.AlternateText = "Descending Order" End If row.Cells(columnIndex).Controls.Add(sortImage) End Subsuperguppie
All-Star
48225 Points
8679 Posts
Re: gridview sorting is not working
Feb 27, 2012 02:51 PM|LINK
I suggest the code that is in Page_Load now. I also suggest you do that code in Page_Load only when IsPostBack is False.
The code to go in the Sorting handler will need to add "order by' and the field to sort on to the query.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
siraero
Member
419 Points
604 Posts
Re: gridview sorting is not working
Feb 27, 2012 08:50 PM|LINK
Hi
I close this Question, i thing i have found another code for this i just need some help for it, so i have made another Question.
But thx anyway ;)