如何在GridView中绑定动态列

时间:2018-01-13 18:01:53

标签: c# asp.net .net

我有一个gridview,我绑定下面的数据,其中@cols给出了表中的动态日期。

但是如何将此列绑定到gridview,因为我们必须在itemtemplate中提供列名?

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>

1 个答案:

答案 0 :(得分:1)

使用BoundField

  

表示在数据绑定控件中显示为文本的字段。

首先设置Gridview autogeneratedcolumns=false;

的属性

尝试以下

BoundField testField= new BoundField();
testField.DataField = "New_testField_Name";
test.Headertext = "testField_Header";
CustomersGridView.Columns.Add(test);

与OP讨论后。

只需保留GridView而不指定任何列。(根据您的要求应用样式和其他属性)

<asp:GridView ID="gridViewAttendance" runat="server" autogeneratecolumns="true">


 </asp:GridView>

CS档案中

超出您的查询并使用GridView绑定结果。

 SqlCommand cmd = new SqlCommand(query, cnn);
 DataTable dt = new DataTable();
 SqlDataAdapter adp = new SqlDataAdapter(cmd);
 adp.Fill(dt);

 gridViewAttendance.DataSource = dt;
 gridViewAttendance.DataBind();

只需执行此操作,查询返回的任何列都将显示在网格中。