tag:blogger.com,1999:blog-6907873803403737979.post8759535853052018020..comments2023-10-16T12:48:46.251+01:00Comments on C# Bits: Setting the Initial Sort Order – Dynamic DataStephen J. Naughtonhttp://www.blogger.com/profile/17435527974910745156noreply@blogger.comBlogger28125tag:blogger.com,1999:blog-6907873803403737979.post-323015809850697602013-04-23T12:09:58.191+01:002013-04-23T12:09:58.191+01:00Hi there, there is a post on my blog called settin...Hi there, there is a post on my blog called setting initial sort order that will do it.<br /><br />Also Cascade should follow the DisplayColumn sort attribute sort column.<br /><br />SteveStephen J. Naughtonhttps://www.blogger.com/profile/17435527974910745156noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-3665853015512403002013-04-23T08:24:07.604+01:002013-04-23T08:24:07.604+01:00Hi Steve,
Great to see your posts. I have been usi...Hi Steve,<br />Great to see your posts. I have been using your cascade filter example. Right now I would like to sort the foreign key filter values that are displayed in every page (list/edit/insert/details) without breaking the cascade effect. I have set the initial sort order of the table using the DisplayColumn attribute. <br />Also, is there a way in which I can choose the order in which the tables in the model get displayed in the main page?trytoreachhighnoreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-80347237273841744252012-11-16T10:23:29.647+00:002012-11-16T10:23:29.647+00:00Hi Johnathan, I actually useht emethod below now a...Hi Johnathan, I actually useht emethod below now as I did have issues with the GidViews sort :)<br /><br />public static void SetInitialSortOrder(this QueryExtender queryExtender, MetaTable table)<br />{<br /> // set default sort<br /> if (table.SortColumn != null)<br /> {<br /> var order = new OrderByExpression()<br /> {<br /> DataField = table.SortColumn.Name,<br /> Direction = table.SortDescending ? SortDirection.Descending : SortDirection.Ascending,<br /> };<br /> queryExtender.Expressions.Add(order);<br /> }<br />}<br /><br />SteveStephen J. Naughtonhttps://www.blogger.com/profile/17435527974910745156noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-55855215537143800542012-11-16T07:41:55.537+00:002012-11-16T07:41:55.537+00:00Is it a wise approach to do something like this in...Is it a wise approach to do something like this in the Page_Load of list.aspx.cs:<br /><br />if (!IsPostBack)<br /> {<br /> if (table.SortColumn != null)<br /> {<br /> GridView1.Sort(table.SortColumn.DisplayName,<br /> table.SortDescending ? SortDirection.Descending : SortDirection.Ascending);<br /> }<br /> }<br /> }<br /><br />Thank you for the insightful article really got me into DD.Anonymoushttps://www.blogger.com/profile/02349202055466609071noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-62782918946231311572012-11-16T07:40:16.119+00:002012-11-16T07:40:16.119+00:00In Page_Load couldn't you simply do:
if (!IsP...In Page_Load couldn't you simply do:<br /><br />if (!IsPostBack)<br /> {<br /> if (table.SortColumn != null)<br /> {<br /> GridView1.Sort(table.SortColumn.DisplayName,<br /> table.SortDescending ? SortDirection.Descending : SortDirection.Ascending);<br /> }<br /> }<br /> }<br /><br />Again thanks for the article it was insightful to start digging more underneath the hood of DD.Anonymoushttps://www.blogger.com/profile/02349202055466609071noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-29188597859904986882012-07-10T09:50:22.691+01:002012-07-10T09:50:22.691+01:00Hi Daniel, if you look at the previous post to you...Hi Daniel, if you look at the previous post to yours there is a link to th latest version :) for .Net 4 and 4.5<br /><br />and you are of course very welcome<br /><br />SteveStephen J. Naughtonhttps://www.blogger.com/profile/17435527974910745156noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-38356508751432229262012-07-10T09:28:58.609+01:002012-07-10T09:28:58.609+01:00Thank you for your post! It was very helpful!Thank you for your post! It was very helpful!Danielnoreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-75161470239145795362012-06-11T16:36:33.219+01:002012-06-11T16:36:33.219+01:00Hi James see http://csharpbits.notaclue.net/2011/0...Hi James see http://csharpbits.notaclue.net/2011/01/setting-initial-sort-order-dynamic-data.html this should be it for .Net 4Stephen J. Naughtonhttps://www.blogger.com/profile/17435527974910745156noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-43747878054111886022012-06-11T14:47:12.241+01:002012-06-11T14:47:12.241+01:00@Steve - where do you add that code in the .Net 4 ...@Steve - where do you add that code in the .Net 4 version? The Page_Load in List.aspx.cs?<br /><br />Thanks!James Manninghttps://www.blogger.com/profile/11430624980281560756noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-39333537652144184512012-01-20T18:51:33.652+00:002012-01-20T18:51:33.652+00:00Glad you like it Jim
SteveGlad you like it Jim<br /><br />SteveStephen J. Naughtonhttps://www.blogger.com/profile/17435527974910745156noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-82121709714021205652012-01-20T18:38:43.769+00:002012-01-20T18:38:43.769+00:00Hi Steve,
I don't know how I found this post ...Hi Steve,<br /><br />I don't know how I found this post but it was a great help, and also got me to learn about extension methods.<br /><br />Thanks again and I'm bookmarking your site.<br /><br />Thanks,<br /><br />JimJimbohttps://www.blogger.com/profile/00317044340390254418noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-47554897906177069002011-08-03T18:48:22.602+01:002011-08-03T18:48:22.602+01:00Hi Jonathan, I don;t do it that way anymore with ....Hi Jonathan, I don;t do it that way anymore with .Net 4. I do it liek this, much more relyable.<br /><br />// set default sort<br />if (table.SortColumn != null)<br />{<br /> var order = new OrderByExpression()<br /> {<br /> DataField = table.SortColumn.Name,<br /> Direction = table.SortDescending ? SortDirection.Descending : SortDirection.Ascending,<br /> };<br /> queryExtender.Expressions.Add(order);<br />}Stephen J. Naughtonhttps://www.blogger.com/profile/17435527974910745156noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-69011992052700514842011-08-03T18:09:15.091+01:002011-08-03T18:09:15.091+01:00To expand on Stuart Whiteford's code example, ...To expand on Stuart Whiteford's code example, the order by syntax when working with an EntityDataSource is slightly different:<br /><br />string orderBy = String.Format("it.{0} {1}", displayColumn.SortColumn, displayColumn.SortDescending ? "desc" : "asc");<br />GridDataSource.OrderBy = orderBy;<br /><br />Also, excluding this sorting code from post backs (as fabiocanada pointed out) allows for re-sorting by the user, but after a delete, for example, the default sort order is lost. The user can then re-sort using the column headers but it's not a good user experience , especially when there are several pages of data.Jonathan Moreaunoreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-53040515712925336212011-03-26T01:07:38.022+00:002011-03-26T01:07:38.022+00:00Glad it does Yuri :)
SteveGlad it does Yuri :)<br /><br />SteveStephen J. Naughtonhttps://www.blogger.com/profile/17435527974910745156noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-78225946975355161982011-03-25T16:59:23.863+00:002011-03-25T16:59:23.863+00:00Hi Steve,
QueryExtender works fine to me.
Thank ...Hi Steve,<br /><br />QueryExtender works fine to me.<br /><br />Thank youUnknownhttps://www.blogger.com/profile/18368917593547953867noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-82557830272875450832011-03-24T19:16:33.246+00:002011-03-24T19:16:33.246+00:00Hi Yuri, you can just apply multiple column name t...Hi Yuri, you can just apply multiple column name to the GridView sort, also if you are using VS2010 and .Net 4 then you can add a sort to QueryExtender that should give you the control.<br /><br />SteveStephen J. Naughtonhttps://www.blogger.com/profile/17435527974910745156noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-66974017177894043022011-03-24T18:31:16.231+00:002011-03-24T18:31:16.231+00:00Hi Steve,
Nice tip, but I have a problem. I nee...Hi Steve,<br /><br /> Nice tip, but I have a problem. I need to do sort using 2 fields. I try to put other "DisplayColumn". Do you know, how I can do that ?<br /><br />ThanksUnknownhttps://www.blogger.com/profile/18368917593547953867noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-70091504040212087172010-11-22T17:04:13.006+00:002010-11-22T17:04:13.006+00:00Good point fabiocanada :)
SteveGood point fabiocanada :)<br /><br />SteveStephen J. Naughtonhttps://www.blogger.com/profile/17435527974910745156noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-82049916397802048482010-11-22T16:51:42.492+00:002010-11-22T16:51:42.492+00:00The code works! But it's better top use it onl...The code works! But it's better top use it only when the page is not a post back. I was able to delete and re-order.<br /><br />protected void Page_Load(object sender, EventArgs e)<br />{<br /> if (!IsPostBack)<br /> {<br /> var displayColumn = table.Attributes.OfType().FirstOrDefault();<br /> if (displayColumn != null && displayColumn.SortColumn != null)<br /> {<br /> GridView1.Sort(displayColumn.SortColumn,<br /> displayColumn.SortDescending ? SortDirection.Descending : SortDirection.Ascending);<br /> }<br /> }<br />}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-84314798520781851082010-04-27T16:57:14.177+01:002010-04-27T16:57:14.177+01:00Thanks fabriciofuji :)
SteveThanks fabriciofuji :)<br /><br />SteveStephen J. Naughtonhttps://www.blogger.com/profile/17435527974910745156noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-29144849698582141132010-04-27T16:24:40.251+01:002010-04-27T16:24:40.251+01:00Worked perfectly! Thanks.Worked perfectly! Thanks.Unknownhttps://www.blogger.com/profile/12926865512420361890noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-69016496903236010222010-02-10T11:18:28.296+00:002010-02-10T11:18:28.296+00:00Thanks again for this - solved my problem!Thanks again for this - solved my problem!Gareth Thomas Hillhttps://www.blogger.com/profile/13811920704142646474noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-14054495213852320922009-11-16T09:47:59.707+00:002009-11-16T09:47:59.707+00:00Hi Stuart, thanks for that, I'll do an update ...Hi Stuart, thanks for that, I'll do an update and cite you for that snippet, do you have a blog or website I can point to in the citation?<br /><br />Steve :DStephen J. Naughtonhttps://www.blogger.com/profile/17435527974910745156noreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-29751592879228269202009-11-16T08:21:32.945+00:002009-11-16T08:21:32.945+00:00not much help for a newbie...not much help for a newbie...Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6907873803403737979.post-82248374277358749862009-07-20T20:59:16.241+01:002009-07-20T20:59:16.241+01:00Steve,
Instead of sorting on the GridView, I set ...Steve,<br /><br />Instead of sorting on the GridView, I set the OrderBy property of the underlying LinqDataSource in The Page_Load method and that seems to resolve the deleting and paging issues.<br /><br />[code]<br />string orderBy = string.Format("{0} {1}", displayColumn.SortColumn, displayColumn.SortDescending ? "descending" : "ascending");<br />GridDataSource.OrderBy = orderBy;<br />[/code]Stuart Whitefordhttp://blog.stuartwhiteford.com/noreply@blogger.com