【问题标题】:How to bind dynamic column in the GridView如何在 GridView 中绑定动态列
【发布时间】: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>

【问题讨论】:

    标签: c# asp.net .net


    【解决方案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();
    

    只需这样做,您的查询返回的任何列都将显示在网格中。

    【讨论】:

    • 主要问题是我在 DataField 中提供什么,因为我不知道列名?
    • 你将从哪里得到`@cols`?
    • 我已经更新了上面的代码。 '@cols' 将有日期。
    • 共享。实际上在显示名称后,应该有日期列(1/1/2018,1/2/2018,.....)。
    • 也试过了,然后什么都不显示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多