Click to See Complete Forum and Search --> : GridView with custom dataset - create link & sorting


ccfriend
January 28th, 2006, 08:07 AM
Hi

I have an aspx page that consumes a web service that return a dataset.

Here is a sample code:


WSSearch temp = new WSSearch(); //Load web service
DataSet temp = temp.Ws_seach(TextBox2Title.Text); //call web method

GridView1Search.DataSource = temp.Tables[0]; //assign the return dataset (the first DataTable)
GridView1Search.DataBind(); //binding stuff


After that my GridView is show all the records that dataset contains

My first problem is that one of the columns contains as data a link and I want to make it a link (that's it to press it and load a http page).
Is there any way to do it?

My second problem is that when I add a statement:
GridView1Search.AllowSorting = true;

And I push the field header in order to sort by field, an exception is thrown. Take a look:



The GridView 'GridView1Search' fired event Sorting which wasn't handled.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The GridView 'GridView1Search' fired event Sorting which wasn't handled.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[HttpException (0x80004005): The GridView 'GridView1Search' fired event Sorting which wasn't handled.]
System.Web.UI.WebControls.GridView.OnSorting(GridViewSortEventArgs e) +236
System.Web.UI.WebControls.GridView.HandleSort(String sortExpression, SortDirection sortDirection) +77
System.Web.UI.WebControls.GridView.HandleSort(String sortExpression) +71
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +545
System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +190
System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +170
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4919


Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42



So there is any way to make the sort work out ?

HairyMonkeyMan
January 30th, 2006, 05:23 AM
As far as creating the link goes; Have a look at 'Hyperlink Columns'. Create them in your presentation layer, when you define your gridview:
<Asp:Gridview ....>
<Columns>
<Asp:HyperlinkField DataNaviagateUrlFields="PrimaryKey" DataNavigateUrlFormatString="DisplayDetails.aspx?id={0}" DataTextField="PrimaryKey" HeaderText="ID" SortExpression="PrimaryKey" />
</Columns>
</Asp:GridView>

As far as your sorting functions go, if you are using late binding to your database, you will need to handle the sorting routines of your gridview. I've always used the early binding data objects in the presentation layer... It automatically handles the sort.

Hope this helps ;)

ccfriend
January 30th, 2006, 11:01 AM
Thanks HairyMonkeyMan

HyperlinkField working great!!!!!!!!

For sorting you must override : OnSorting (i always knew about the easy, fast visual way to do it)

Thanks again