Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 gridview does not sort

Author  Topic 

rajani
Constraint Violating Yak Guru

367 Posts

Posted - 2005-10-11 : 15:46:50
Hi friends
i have gridview that is being populated thru button click.it displays data correctly but problems comes with sorting.
i have gone thru quick start samples which work fine but they use sqldatasource.in my case am populating the gridview thru command click
is it any different between them when sorting?
my code for gridview

<asp:GridView ID="grdTasks" AllowSorting="True" AllowPaging="True" runat="server"

DataKeyNames="taskid" AutoGenerateColumns="False" Width="427px" OnSorting="grdTasks_Sorting" OnSorted="grdTasks_Sorted" OnSelectedIndexChanged="grdTasks_SelectedIndexChanged" OnPageIndexChanged="grdTasks_PageIndexChanged">

<Columns>

<asp:CommandField ShowSelectButton="true" />

<asp:BoundField DataField="taskid" HeaderText="taskid" ReadOnly="True" SortExpression="taskid" />

<asp:BoundField DataField="entrydt" HeaderText="entrydt" SortExpression="entrydt" />

<asp:BoundField DataField="taskname" HeaderText="taskname" SortExpression="taskname" />

<asp:BoundField DataField="completed" HeaderText="completed" SortExpression="completed" />

</Columns>

</asp:GridView>

my button click code


protected void btnSearch_Click(object sender, EventArgs e)

{

CustomerOrders cs = new CustomerOrders();

DataSet ds= cs.FetchTasks(DropDownList1.Text);

grdTasks.DataSource = ds;

grdTasks.DataBind();

}

but when click on grid header nothing happens .it was giving error saying Sorting event was not handled so added that event too.the code i added is
grdTasks.DataBind();

any one has ideas to fix this??

Thanks for the help.(BTW am using VS2005 RC1 and XP prof)



Cheers

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-10-11 : 16:33:28
You need to explicitly sort the datasource of the grid to get it to sort; it doesn't do this for you automatically.

For example, if your grid is bound to a DataTable, then you would do something like this on the Sort() event:

DataTable t = (DataTable) grdTasks.DataSource;
t.DefaultView.Sort="<sort expression here>";
grdTasks.DataBind()

Go to Top of Page

rajani
Constraint Violating Yak Guru

367 Posts

Posted - 2005-10-11 : 17:07:12
Thanks for the post mate. i tried that like this
DataTable dt=(DataTable)grdTasks.DataSource;
dt.DefaultView.Sort = "taskid";
//grdTasks.Sort(e.SortExpression.ToString(),e.SortDirection+1);

grdTasks.DataBind();
but getting following error
Object reference not set to an instance of an object.
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.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 30: {
Line 31: DataTable dt=(DataTable)grdTasks.DataSource;
Line 32: dt.DefaultView.Sort = "taskid";
Line 33: //grdTasks.Sort(e.SortExpression.ToString(),e.SortDirection+1);
Line 34:



Cheers
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-10-11 : 20:21:37
You might need to re-fill the datatable first, then set the sort expression, then databind() ...
Go to Top of Page

rajani
Constraint Violating Yak Guru

367 Posts

Posted - 2005-10-11 : 20:24:59
could you kindly post some sample code please or any web reference with regard to this much appreciated
Thanks

Cheers
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-10-11 : 20:56:34
As always, remember that Google is your friend:

http://www.google.com/search?q=datagrid+sort

That search returned 4 or 5 good articles right on the first page at first glance.
Go to Top of Page

rajani
Constraint Violating Yak Guru

367 Posts

Posted - 2005-10-11 : 21:02:49
Guess what smith following line always returns Null !! i think thats what the problem for the abv error.any ideas
DataTable dt=(DataTable)grdTasks.DataSource;
so when i debug dt does not contain anything when i see it watch windwo.
BTW i can see data on the grid though

Cheers
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-10-12 : 07:44:15
Again, you probably need to re-fill the datatable on the sort event. See the examples. Also, yes, that error message means that some object you are trying to reference is set to null.
Go to Top of Page

rajani
Constraint Violating Yak Guru

367 Posts

Posted - 2005-10-12 : 16:05:14
i finally found that data source does not persist the between posts to the server so i stored it in session state.
am populating session like following in button click where i populate the grid
Session["StoreData"] = ds;

and my changed code in grid sorting event.
DataSet ds = (DataSet)Session["StoreData"];
ds.Tables[0].DefaultView.Sort="taskid DESC";
grdTasks.DataBind();

now i can see "ds" has some data which is good news for me after no of days but after last command (ie grdTasks.DataBind()) the grid goes blank .
it is as if i made filter and filter yielded no records .any ideas please.


Cheers
Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2005-10-12 : 16:11:00
Instead of
DataTable t = (DataTable) grdTasks.DataSource;
t.DefaultView.Sort="<sort expression here>";


Try
ds.Tables[0].DefaultView.Sort = "<sort expression>"

before you call DataBind.

EDIT: Damn! on that part...


Also, make sure that you are not calling DataBind on Postback from your Page_Load event. This is a common mistake and it will override the DataBind from your sort routine.

---------------------------
EmeraldCityDomains.com
Go to Top of Page

rajani
Constraint Violating Yak Guru

367 Posts

Posted - 2005-10-12 : 16:29:18
Hi AjarnMark
you said
>>Try
>>ds.Tables[0].DefaultView.Sort = "<sort expression>"
>>before you call DataBind.
but thats what i am doing currently ,am not I?
BTW i am not populating gridview in page load it is only in button click so should be fine i believe.

Cheers
Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2005-10-12 : 17:32:27
Rajani, yes, you got your update posted while I was working on mine, so we crossed paths.

---------------------------
EmeraldCityDomains.com
Go to Top of Page

rajani
Constraint Violating Yak Guru

367 Posts

Posted - 2005-10-12 : 17:40:19
Thanks for the post mate.i finally got it sorted :)
this is what i did
// Retrieve the data source from session state.
DataTable dt = (DataTable)Session["StoreData"];

// Create a DataView from the DataTable.
DataView dv = new DataView(dt);

// The DataView provides an easy way to sort. Simply set the
// Sort property with the name of the field to sort by.
dv.Sort = e.SortExpression;

// Rebind the data source and specify that it should be sorted
// by the field specified in the SortExpression property.
grdTasks.DataSource = dv;
grdTasks.DataBind();
it works quite nicely

Cheers
Go to Top of Page
   

- Advertisement -