The GridView 'GridView1' fired event Sorting which wasn't handled

A

Arjen

Hi,

I get this error message when sorting a gridview:
The GridView 'GridView1' fired event Sorting which wasn't handled

What do I need to do?

Thanks!
 
T

Teemu Keiski

Hi,

you have AllowSorting="true" set for the GridView and therefore you need to
have event handler for its Sorting event

<asp:GridView AllowSorting=true ID="GridView1" runat="server"
OnSorting="GridView1_Sorting">
...
</asp:GridView>


protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//...
}
 
A

Arjen

I tried, I don't get it to work.
I don't know what to add inside the GridView1_Sorting method.

I'm using the gridview with templatefields.

Can you help me?

Thanks!
 
Joined
Nov 1, 2007
Messages
1
Reaction score
0
Solution

I came across your question while seeking an answer to the exact same situation. I found a solution elsewhere ... not exactly what you were looking for you do have to create handlers but here's some nice generic ones...

<asp:GridView ID="gridView" OnPageIndexChanging="gridView_PageIndexChanging"

OnSorting="gridView_Sorting" runat="server" />

private string ConvertSortDirectionToSql(SortDirection sortDireciton)
{
string m_SortDirection = String.Empty;

switch (sortDirection)
{
case SortDirection.Ascending:
m_SortDirection = "ASC";
break;

case SortDirection.Descending:
m_SortDirection = "DESC";
break;
}

return m_SortDirection
}

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridView.PageIndex = e.NewPageIndex;
gridView.DataBind();
}

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable m_DataTable = gridView.DataSource as DataTable;

if (m_DataTable != null)
{
DataView m_DataView = new DataView(m_DataTable);
m_DataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

gridView.DataSource = m_DataView;
gridView.DataBind();
}
}


I had to tweak this up a bit, as gridview datasource was null ... I went back through my custom SQL build and did DataBind() again and it works !

Good luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top