【发布时间】:2020-09-27 02:56:46
【问题描述】:
我有一个网格视图,我在其中绑定以下数据,@cols 为我提供了表格中存在的动态日期。
但是由于我们必须在 itemtemplate 中提供列名,如何将此列绑定到 gridview?
WITH cte (startdate)
AS
(SELECT
@startdate AS startdate
UNION ALL
SELECT
DATEADD(DD, 1, startdate) AS startdate
FROM cte
WHERE startdate < @enddate
)
select c.startdate
into #tempDates
from cte c
where datename(weekday, c.startdate) <> 'Sunday';
SELECT
@cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(CONVERT(CHAR(10),startdate, 105))
FROM #tempDates
FOR XML PATH (''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
SET @query = 'SELECT RollNo,FirstName,LastName, ' + @cols + ' from
(
select S.RollNo,U.FirstName,U.LastName,
D.startdate,
convert(CHAR(10), startdate, 105) PivotDate
from #tempDates D,Attendance A, Student S, UserDetails U
where convert(CHAR(10), D.startdate, 105) = convert(CHAR(10), A.Date, 105) and A.EnrollmentNo=S.EnrollmentNo and A.EnrollmentNo=U.userID
) x
pivot
(
count(startdate)
for PivotDate in (' + @cols + ')
) p '
EXECUTE (@query)
Gridview-
<asp:GridView ID="gridViewAttendance" Style="width: 600px;margin-top:50px; " runat="server" AutoGenerateColumns="False" Visible="False" AllowPaging="True" CellPadding="2" CellSpacing="2" PageSize="20" HorizontalAlign="Center" EmptyDataText="No Records Found" Font-Size="Small" ShowHeaderWhenEmpty="True" AllowSorting="True">
<Columns>
<asp:TemplateField HeaderText="Roll No">
<ItemTemplate>
<asp:Label runat="server" ID="lblRNo" Text='<%# Eval("RollNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label runat="server" ID="lblName" Text='<%# String.Format("{0} {1}", Eval("FirstName"), Eval("LastName")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="??">
<ItemTemplate>
<asp:Label runat="server" ID="??" Text='<%# Eval("??") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:GridView>
【问题讨论】: