[英]Populating a GridView
假设您具有下表:
LogID UserID Date Action
1 Bob 2013-05-23 13:30:27 1
2 Bill 2013-05-23 13:31:36 1
3 Bob 2013-05-23 13:32:45 2
4 Bill 2013-05-23 13:35:12 2
然后,您创建一个GridView
并在Date
和Action
列中进行填充:
Date Action
2013-05-23 13:30:27 1
2013-05-23 13:31:36 1
2013-05-23 13:32:45 2
2013-05-23 13:35:12 2
您可以怎么做(在GridView
)而不是在Action
下显示1
或2
,而是分别显示Clock-In
或Clock-Out
?
提前致谢!
这是我的查询(标识符与问题无关)。 我在哪里可以在此处添加案例说明?
var Entries = from entries in db.EmployeeTimeClocks
where entries.Date.Value.Day == DateTime.Now.Day &&
entries.UserId == AppCommon.Security.CurrentUser.UserId
orderby entries.ID descending
select entries;
您可以使用案例陈述
http://sqlfiddle.com/#!6/aa91e/3
这是SQL
SELECT LogID, UserId, Date,
CASE Action
WHEN 1 THEN 'Clock-In'
ELSE 'Clock-Out'
END
FROM MyTable
编辑
使用LINQ to SQL,您将使用select作为
var Entries = from entries in db.EmployeeTimeClocks
where entries.Date.Value.Day == DateTime.Now.Day &&
entries.UserId == AppCommon.Security.CurrentUser.UserId
orderby entries.ID descending
select new {
UserId = entries.UserId,
//Other fields
Clock = (entries.ClockId == 1 ? "Clock In" : "Clock Out")
};
我不知道你想怎么做。 但是,如果你只有两个动作是1和2(或缩小),你应该打电话给你列“同步的,在”,使之成为Bit
类型。
您应该在SQL端执行此操作。
您可以简单地与此关系创建另一个表( ForeignKey ):
Actions
| ID | Name |
------------------
|1 | Clock-In |
|2 | Clock-Out |
您还可以使用返回正确名称的case语句更改查询。
我在这里没有您的查询,所以这只是一个例子:
SELECT CASE Action WHEN 1 THEN 'Clock-In' ELSE 'Clock-Out' END
在这种情况下,请确保只有两个可能的选项是1或2。
您可以拥有模板列,然后像这样设置文本
<asp:TemplateField HeaderText="Action" >
<ItemTemplate><%# (Eval("Action")==1) ? "ClockIn" : "ClockOut" %></ItemTemplate>
</asp:TemplateField>
如果要使用代码隐藏,请尝试查看GridView的RowDataBound事件
protected void GridView1_RowDatabound(object source, GridItemEventArgs e)
{
try
{
//Get Row Data
//if(value == 1/2)
//re-assign to DataGrid's Row
}
catch (Exception exception)
{
//HandleException
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.