4

enter image description hereI'm making a search list through a grid in asp.net web form. I am using web service for loading the grid. Now how can I ascending the Request Date ??

 gvproposal.DataSource = webservice.EnquiryGetAllLcRequest();
 gvproposal.DataBind();

here I simply bind the web service now how can I ascending the date??

public IS_LC_REQUEST[] EnquiryGetAllLcRequest();

this is web service method.

 public class IS_LC_REQUEST : CModelBaseOfIS_LC_REQUEST
    {
        public IS_LC_REQUEST();
        public string BENEFICIARY_ADDRESS { get; set; }
        public string BENEFICIARY_NAME { get; set; }
        public string BRANCH_ID { get; set; }
        public string PORT_OF_SHIPMENT { get; set; }
        public string REQUEST_DATE { get; set; }
        public string REQUEST_ID { get; set; }

    }

Updated:

var arrayOfObjects = IntBankProposal.EnquiryGetAllLcRequest();
var dt = DateTime.Now;
gvproposal.DataSource = arrayOfObjects.OrderBy(load => { if (DateTime.TryParse(load.REQUEST_DATE, out dt)) { return dt; } else { return DateTime.Now.AddYears(-100); } }).ToArray();
gvproposal.DataBind();
S.Rusdana
  • 249
  • 1
  • 2
  • 12

4 Answers4

2

You can use LINQ to sort the collection you receive from web service call. Since REQUEST_DATE is of string type, you need to convert it to date using Date.Parse. Make sure that you include this namespace in your code using System.Linq;

The code below assumes that REQUEST_DATE string contains valid date values

var arrayOfObjects =  webservice.EnquiryGetAllLcRequest();
gvproposal.DataSource = arrayOfObjects.OrderBy(e => DateTime.Parse(e.REQUEST_DATE)).ToArray();
gvproposal.DataBind();

If you expect invalid date values in REQUEST_DATE, then use the code snippet below

var arrayOfObjects =  webservice.EnquiryGetAllLcRequest();
var dt = DateTime.MinValue;
DateTime dtNull = DateTime.Now.AddYears(-100);
gvproposal.DataSource = arrayOfObjects.OrderBy(e => { dt = DateTime.MinValue; if(DateTime.TryParse(e.REQUEST_DATE, out dt)) { return dt;} else { return dtNull;}}).ToArray();
gvproposal.DataBind();

Another code snippet when invalid date strings could be there is as below.Try this also.

var arrayOfObjects =  webservice.EnquiryGetAllLcRequest();
var dt = DateTime.MinValue;
 gvproposal.DataSource = arrayOfObjects.OrderBy(e => { dt = DateTime.MinValue; DateTime.TryParse(e.REQUEST_DATE, out dt); return dt;}).ToArray();
gvproposal.DataBind();

I tried the above code on my machine using an object like the one you have and it worked perfectly as you can see in screen shot belwo.

Sort on Object's Date Property for Array of Objects using LINQ

Sunil
  • 20,653
  • 28
  • 112
  • 197
  • Thank you.Your answer work for me.I just need some edit to work. If i remove e to something else and remove the Date.Parse. It works smoothly. – S.Rusdana Jan 13 '16 at 05:53
  • can you please help me to ascending the date. `REQUEST_DATE` is string. So i want to ascending as date.And if i write `Date.Parse(e.REQUEST_DATE)` then it gives `The name 'Date' does not exist in the current context.` – S.Rusdana Jan 13 '16 at 06:13
  • Can you convert the string type to DateTime in the web service which returns the collection? So the class `IS_LC_REQUEST` is defined so `REQUEST_DATE` is of DateTime type. – Sunil Jan 13 '16 at 06:56
  • No..i can't convert it.If I write `gvproposal.DataSource = arrayOfObjects.OrderBy(load => Convert.ToDateTime(load.REQUEST_DATE)).ToArray();` It says string is not valid – S.Rusdana Jan 13 '16 at 07:10
  • That should work using Convert. What error do you see? Just use DateTime instead. I will correct it. – Sunil Jan 13 '16 at 07:11
  • Use the latest code I have in which I use DateTime.Parse vs Date.Parse. It should work. – Sunil Jan 13 '16 at 07:13
  • String was not recognized as a valid DateTime. – S.Rusdana Jan 13 '16 at 07:15
  • I think you have invalid datetime values in REQUEST_DATE. – Sunil Jan 13 '16 at 07:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100547/discussion-between-s-rusdana-and-sunil). – S.Rusdana Jan 13 '16 at 07:17
  • Let me provide a code snippet to deal with invalid values. Then let's go there. Give me a minute. – Sunil Jan 13 '16 at 07:18
  • How can I convert to valid string date? – S.Rusdana Jan 13 '16 at 07:20
  • Look at second code snippet I have added which will take care of invalid date values and put them at the top of sort order like a query puts null values at the top. – Sunil Jan 13 '16 at 07:25
  • I have tried the second code snippet in LINQPad and it works with invalid date values. – Sunil Jan 13 '16 at 07:26
  • `var arrayOfObjects = webservice.EnquiryGetAllLcRequest(); gvproposal.DataSource = arrayOfObjects.OrderBy(e => DateTime.Parse(e.REQUEST_DATE)).ToArray(); gvproposal.DataBind();`this one??Sorry this one not working. – S.Rusdana Jan 13 '16 at 07:30
  • No. I have another code snippet that I just added. Please use the second code snippet in my answer. You are still using the first code snippet which will only work when the date values are valid in REQUEST_DATE. – Sunil Jan 13 '16 at 07:31
  • I add a photo in the question.You can see that the order comes like that after the second code. – S.Rusdana Jan 13 '16 at 07:40
  • That is ascending order i.e. earlier date comes before later date. So it's correct. – Sunil Jan 13 '16 at 07:41
  • You can optimize it further by moving `var dt = DateTime.Now;` to just before the second line. – Sunil Jan 13 '16 at 07:43
  • 12/13/2015,12/15/2015,12/17/2015.I thing It should the order. – S.Rusdana Jan 13 '16 at 07:44
  • yes that order is ascending order. Its Dec 13th 2015, Dec 15th 2015, Dec 17th 2015 etc. – Sunil Jan 13 '16 at 07:45
  • That should not happen. I don't know what code you are running. I am 100% sure you are not running this code correctly. Can you paste all your code so I can see where you are making a mistake? May be you are calling the same code twice one after the other. – Sunil Jan 13 '16 at 07:51
  • Ok. Can you paste the actual value of REQUEST_DATE for the fifth, sixth and seventh records in the image you added? I am curious to know what those strings are. – Sunil Jan 13 '16 at 07:56
  • Also, in which event do you call this data binding code in your code-behind? – Sunil Jan 13 '16 at 08:00
  • You can come to discussion else this will take too long. I suspect there is some other code that could be causing this. – Sunil Jan 13 '16 at 08:02
  • I am sorry I have to leave if you are busy now, but that second code snippet works as I tested it in LINQPAD on my computer. – Sunil Jan 13 '16 at 08:05
1

Use OrderBy method of linq

gvproposal.DataSource = webservice.EnquiryGetAllLcRequest().ToList().OrderBy(x=>x.REQUEST_DATE);
gvproposal.DataBind();
Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
  • 'INewwsEnquery.IS_LC_REQUEST[]' does not contain a definition for 'OrderBy' and no extension method 'OrderBy' accepting a first argument of type 'INewwsEnquery.IS_LC_REQUEST[]' could be found (are you missing a using directive or an assembly reference?). It shows error :( – S.Rusdana Jan 13 '16 at 05:16
  • 'System.Array' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?) – S.Rusdana Jan 13 '16 at 05:32
  • @S.Rusdana Try `webservice.EnquiryGetAllLcRequest().AsEnumerable().ToList()` before assigning `ToList()` property. – Suprabhat Biswal Jan 13 '16 at 05:37
1

Set AllowSorting property to true, add OnSorting event to the gridview. Then add conditions for sorting in OnSorting event. You can try this reference for details.

Suvro
  • 352
  • 2
  • 13
1

If you want to sort ascending order on date filed. Do, as following.

<asp:GridView ID="grdHeader" AllowSorting="true" AllowPaging="false" 
    AutoGenerateColumns="false" Width="780" runat="server"  OnSorting="grdHeader_OnSorting" EnableViewState="true">
    <Columns>
        <asp:BoundField DataField="REQUEST_DATE" HeaderText="Date" SortExpression="REQUEST_DATE" />

    </Columns>
</asp:GridView>

By default gridview is ascending order. For more information, please visit. GridView sorting: SortDirection always Ascending

Community
  • 1
  • 1
Mahedee
  • 166
  • 7