列在webgrid中不按顺序出现

时间:2014-01-31 13:16:47

标签: asp.net sql asp.net-mvc c#-4.0 webgrid

我从sql store过程中选择名称以在WEBGRID中显示它。我将MVC 3和linq用于sql。

但问题是当我从数据库中选择列名时,它不会像在数据库中那样被排序。

e.g。在数据库中它显示为

EmpName     EmpID     EmpShift

但在webgrid中显示

Empid     EmpShift    EmpID

查询:

with times as (
SELECT    t1.EmplID
        , t3.EmplName
        , min(t1.RecTime) AS InTime
        , max(t2.RecTime) AS [TimeOut]
        , t4.ShiftId as ShiftID
        , t4.StAtdTime as ShStartTime
        , t4.EndAtdTime as ShEndTime
        , cast(min(t1.RecTime) as datetime) AS InTimeSub
        , cast(max(t2.RecTime) as datetime) AS TimeOutSub
        , t1.RecDate AS [DateVisited]
FROM  AtdRecord t1 
INNER JOIN 
      AtdRecord t2 
ON    t1.EmplID = t2.EmplID 
AND   t1.RecDate = t2.RecDate
AND   t1.RecTime < t2.RecTime
inner join 
      HrEmployee t3 
ON    t3.EmplID = t1.EmplID 
inner join AtdShiftSect t4
ON t3.ShiftId = t4.ShiftId
group by 
          t1.EmplID
        , t3.EmplName
        , t1.RecDate
        , t4.ShiftId 
        , t4.StAtdTime 
        , t4.EndAtdTime
)
SELECT 
 EmplID
,EmplName
,ShiftId As ShiftID
,InTime
,[TimeOut]
,convert(char(5),cast([TimeOutSub] - InTimeSub as time), 108) TotalWorkingTime
,[DateVisited]
,CONVERT(char(5),CASE WHEN  CAST([TimeOutSub] AS DATETIME) >= ShEndTime And ShiftID = 'S002'
     Then  LEFT(CONVERT(varchar(12), DATEADD(ms, DATEDIFF(ms, CAST(ShEndTime AS DATETIME),  CAST([TimeOutSub] AS DATETIME)),0), 108),5) 
     WHEN  CAST([TimeOutSub] AS DATETIME) >= ShEndTime And ShiftID = 'S001'
     Then  LEFT(CONVERT(varchar(12), DATEADD(ms, DATEDIFF(ms, CAST(ShEndTime AS DATETIME),  CAST([TimeOutSub] AS DATETIME)),0), 108),5) 
     ELSE '00:00' END, 108) AS OverTime
FROM times order by EmplID, ShiftID, DateVisited

Webgrid输出:

enter image description here

webgrid代码:

public ActionResult showAllEmployees() 
        {
            IEnumerable<GetAtdRecord_SpResult> EmployeeAtd = DataContext.GetAtdRecord_Sp(null).ToList();
            var names = (from n in DataContext.HrEmployees select n).Distinct();
            return View(EmployeeAtd);
        }

视图:

@{

    var grid = new WebGrid(ViewData.Model);

}

@grid.GetHtml()

1 个答案:

答案 0 :(得分:3)

默认WebGrid排序列顺序基于字母顺序。 To make it your own order, you have to specify columns in the WebGrid.