Click here to Skip to main content
15,922,523 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello
i am working with jqgrid in mvc.
i want to convert my date to persian date in my table.
i am using this code for that.
this is my model code:
  public OrderModel()
        {
            OrdersGrid = new JQGrid
            {
                Columns = new List<JQGridColumn>()
                                 {
                                     new JQGridColumn { DataField = "OrderID", 
                                                        // always set PrimaryKey for Add,Edit,Delete operations
                                                        // if not set, the first column will be assumed as primary key
                                                        PrimaryKey = true,
                                                        Editable = false,
                                                        Width = 50 },
                                     new JQGridColumn { DataField = "OrderDate", 
                                                        Editable = true,
                                                        Width = 100, 
                                                        DataType = typeof(DateTime),
                                                        DataFormatString = "{0:d}",
                                                        

                                                        
                                     },
                                     new JQGridColumn { DataField = "CustomerID", 
                                                        Editable = true,
                                                        Width = 100 },
                                     new JQGridColumn { DataField = "Freight", 
                                                        Editable = true,
                                                        Width = 75 },
                                     new JQGridColumn { DataField = "ShipName",
                                                        Editable =  true,
},
                                     new JQGridColumn { DataField = "ShipAddress",
                                                        Editable =  true,
                                                      }                                     
                                 },
                Width = Unit.Pixel(1000),
                Height = Unit.Pixel(400)
            };

            OrdersGrid.ToolBarSettings.ShowRefreshButton = true;
        }

        public JQGrid OrdersGrid { get; set; }
    }
}


my conversion code :
public static string ToPersianDate(this DateTime date)
        {
            var dateTime = new DateTime(date.Year, date.Month, date.Day, new GregorianCalendar());
            var persianCalendar = new PersianCalendar();
            return persianCalendar.GetYear(dateTime) + "/" +
                   persianCalendar.GetMonth(dateTime).ToString("00") + "/" +
                   persianCalendar.GetDayOfMonth(dateTime).ToString("00");
        }

and this is my server side code :
        public ActionResult GridDemo()
        {
   
            var gridModel = new OrderModel();
            var ordersGrid = gridModel.OrdersGrid;
        
            S/etUpGrid(ordersGrid);

          //my code for convert date...

            var list = new List<OrderModel>();
            
    NorthWindsDataContext ss = new NorthWindsDataContext();

            gridModel = (list.Select(t => new
            {

                OrderID = t.OrdersGrid.OrderID,
                OrderDate = t.OrderDate == null
                    ? ""
                    : new PersianDateTime(t.OrderDate.Value)
                        .ToString(PersianDateTimeFormat.DateShortTime)

            }))
            .ToList();



and so Other code for convert code ...

                   //OrdersGrid = new{product => new JQGrid
                   // {
                    
                   //     product.Id.ToString(CultureInfo.InvariantCulture),
                   //     product.Name,
                   //     product.DateTimeAdDateTime.ToPersianDate(),
                   //     product.Price.ToString(CultureInfo.InvariantCulture)
                    
                   // }

           // };

            // Pass the custmomized grid model to the View
            

            return View(gridModel);
        }


but they are not correct and that does not detect OrderDate.

how shoud i modify my code...
pls give me a sample code.

thank you so much from answer.
Posted
Comments
mahmoodof 27-Apr-15 13:16pm    
if i change my code to this i will get this error:
my code:
var list = new List<ordermodel>();

NorthWindsDataContext ss = new NorthWindsDataContext();

gridModel = (ss.Orders.Select(t => new
{

// OrderID = t.OrderID,
OrderDate = t.OrderDate == null
? ""
: new PersianDateTime(t.OrderDate.Value)
.ToString(PersianDateTimeFormat.DateShortTime)

}))
.ToList();


Error:


"Cannot implicitly convert type 'System.Collections.Generic.List<anonymoustype#1>' to 'MainJqGridModelFirst.Models.OrderModel' F:\Project\project 1\ASP MVC 4\Testmvc\MainJqGridModelFirst - Copy(2)\MainJqGridModelFirst\Controllers\OrderTableController.cs"
mahmoodof 27-Apr-15 13:57pm    
this is The little part of setupGrid method .
can i convert my date from here?
my setup method and settings for OrderDate:
//dateTime
var orderDateColumn = ordersGrid.Columns.Find(c => c.DataField == "OrderDate");
orderDateColumn.DataFormatString = "{0:dd/MM/yyyy}";

orderDateColumn.SearchType = SearchType.DatePicker;
// orderDateColumn.DataType = typeof(DateTime);
orderDateColumn.SearchControlID = "DatePicker";
orderDateColumn.SearchToolBarOperation = SearchOperation.IsEqualTo;

One possible solution would be this: internally, in the database, use regular time data types, such as DATE. For presentations, in the applications, use Persian calendar through the class System.Globalization.PersianCalendar:
https://msdn.microsoft.com/en-us/library/system.globalization.persiancalendar%28v=vs.110%29.aspx[^].

You could convert the time structures everytime you get time from database and need to represent it in Persian calendar, or when you get user input in Persian calendar and want to add/modify database data. The concept of time itself is culture-neutral; you can always represent type in different culture-dependent systems.

—SA
 
Share this answer
 
Replace
gridModel = (list


with
ordersGrid.Columns = (list


The problem is: you're assigning the list to the OrdersGrid object, the list needs to be assigned to the Columns property (which is a List<jqgridcolumn>) on the OrdersGrid object, a property of the OrderModel object.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900