【发布时间】:2016-02-10 05:00:33
【问题描述】:
以编程方式向 GridView 添加列很容易。
Here 是一个类似的问题,但它的回答方式相同 - 创建一个数据表并将其绑定到 GridView。
但这不会删除指向主数据源的原始链接吗?
我下面的GridView 已经绑定到它下面的sqldatasource。
Text="Location Board" Width="100%"></asp:Label>
<asp:GridView ID="gvLocationBoard" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" CellPadding="4" DataSourceID="sdsLocationBoard"
ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
...
...
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="sdsLocationBoard" runat="server" ConnectionString="<%$ ConnectionStrings:ATCNTV1ConnectionString %>"
SelectCommand="SELECT rce.RollCallID, v1.StudentID, v1.StudentPreferredName + ' ' + v1.StudentFamilyName AS StudentName, cs.CheckStatusName, rce.DateSubmitted, rce.StaffID, rcp.RollCallPeriod, rcp.PeriodStart, rcp.PeriodEnd FROM vwBoardingTenants AS v1 LEFT OUTER JOIN tblBoardingRollCallEntries AS rce ON rce.StudentID = v1.StudentID LEFT OUTER JOIN tblBoardingCheckStatus AS cs ON cs.CheckStatusID = rce.CheckStatusID LEFT OUTER JOIN tblBoardingRollCallPeriods AS rcp ON rcp.RollCallPeriodID = rce.RollCallPeriodID AND rcp.RowStatus = 1 AND rcp.PeriodYear = YEAR(@SelectedDate) AND dbo.fnDateOnly(@SelectedDate) BETWEEN rcp.PeriodStart AND rcp.PeriodEnd WHERE (dbo.fnDateOnly(rce.DateSubmitted) = dbo.fnDateOnly(@SelectedDate)) AND (v1.Year = YEAR(@SelectedDate))">
<SelectParameters>
<asp:ControlParameter ControlID="txtSelectedDate" Name="SelectedDate" Type="DateTime"/>
</SelectParameters>
</asp:SqlDataSource>
但是,如果您尝试将列标题添加到基于与主数据源完全分离(单独)的数据源的网格视图中,对于整个 GridView?
这是我的“列”sql 数据源...
<asp:SqlDataSource ID="sdsRollCallPeriods" runat="server" ConnectionString="<%$ ConnectionStrings:ATCNTV1ConnectionString %>"
SelectCommand="SELECT RollCallPeriodID, RollCallPeriod, PeriodYear, PeriodStart, PeriodEnd, RowStatus FROM tblBoardingRollCallPeriods WHERE (RowStatus = 1) AND (PeriodYear = @PeriodYear)"
OnSelecting="sdsRollCallPeriods_Selecting">
<SelectParameters>
<asp:Parameter Name="PeriodYear" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
我最终想要的是这样的 Gridview ...
右边的复选框和时间,作为标题,是我要基于sdsRollCallPeriodsSqlDataSource 添加的新列。
【问题讨论】:
-
您始终可以在包含复选框的
ItemTemplate中以声明方式添加列。然后通过Eval或在代码隐藏中设置他们的Checked-state。最好的地方是GridView的RowDataBound活动。您必须使用(CheckBox)row.FindControl("CheckBoxId")来获取参考。底层数据源可通过row.DataItem访问。