GridView:如何根据来自单独数据源的数据添加列
GridView: how to add columns based on data from a separate data source
以编程方式向 GridView 添加列很容易。
有例子here and here.
Here 是一个类似的问题,但答案是一样的——创建一个数据表并将其绑定到 GridView。
但这不会将原始 link 移至主数据源吗?
我下面的 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,如果您尝试将列 headers 添加到基于与主数据源完全独立(单独)的数据源的 gridview,该怎么办?
这是我的 "column" 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 ...
右边的复选框和时间,如headers,是我要根据sdsRollCallPeriods
SqlDataSource 添加的新列。
嗯,这不是真正的解决方案,而是一种变通方法,它对我有用。
我已经添加了我需要的列,并将它们全部设置为 Visible="false"
。
<asp:GridView ID="gvLocationBoard" runat="server" AllowPaging="True" AllowSorting="True" ShowFooter="false" ShowHeader="true" Visible="true" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None"
DataSourceID="sdsLocationBoard" OnDataBound="gvLocationBoard_DataBound" OnRowDataBound="gvLocationBoard_RowDataBound" PageSize="15">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="RollCallID" InsertVisible="False" SortExpression="RollCallID"
Visible="False">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("RollCallID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StudentID" SortExpression="StudentID" Visible="False">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("StudentID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student" SortExpression="StudentName">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("StudentName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" SortExpression="CheckStatusName" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("CheckStatusName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RollCallPeriod0" Visible="False">
<ItemTemplate>
<asp:CheckBox ID="cbRollCallPeriod0" runat="server" />
<asp:HiddenField ID="hfRollCallPeriod0" runat="server" Value='<%# Eval("RollCallPeriod") %>' />
</ItemTemplate>
<HeaderStyle Font-Size="Small" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="RollCallPeriod1" Visible="False">
<ItemTemplate>
<asp:CheckBox ID="cbRollCallPeriod1" runat="server" />
<asp:HiddenField ID="hfRollCallPeriod1" runat="server" Value='<%# Eval("RollCallPeriod") %>' />
</ItemTemplate>
<HeaderStyle Font-Size="Small" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
..
etc with the RollCallPeriod2, 3, 4 and so on, as many as required.
然后我会在 Page_Load()
上打开它们,具体取决于我需要的一天的时间。
DataSourceSelectArguments args = new DataSourceSelectArguments();
DataView dv = sdsRollCallPeriods.Select(args) as DataView;
DataTable dt = dv.ToTable() as DataTable;
if (dt != null)
{
int wsCol = 4; //start of PeriodTimes column in gvLocationBoard
int wsPos = 0;
foreach (DataRow dr in dt.Rows)
{
gvLocationBoard.Columns[wsCol + wsPos].HeaderText = dr.ItemArray[1].ToString();
gvLocationBoard.Columns[wsCol + wsPos].Visible = !gvLocationBoard.Columns[wsCol + wsPos].HeaderText.StartsWith("RollCallPeriod");
wsPos += 1;
}
}
瞧,我有我的行和我的动态列!
以编程方式向 GridView 添加列很容易。
有例子here and here.
Here 是一个类似的问题,但答案是一样的——创建一个数据表并将其绑定到 GridView。
但这不会将原始 link 移至主数据源吗?
我下面的 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,如果您尝试将列 headers 添加到基于与主数据源完全独立(单独)的数据源的 gridview,该怎么办?
这是我的 "column" 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 ...
右边的复选框和时间,如headers,是我要根据sdsRollCallPeriods
SqlDataSource 添加的新列。
嗯,这不是真正的解决方案,而是一种变通方法,它对我有用。
我已经添加了我需要的列,并将它们全部设置为 Visible="false"
。
<asp:GridView ID="gvLocationBoard" runat="server" AllowPaging="True" AllowSorting="True" ShowFooter="false" ShowHeader="true" Visible="true" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None"
DataSourceID="sdsLocationBoard" OnDataBound="gvLocationBoard_DataBound" OnRowDataBound="gvLocationBoard_RowDataBound" PageSize="15">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="RollCallID" InsertVisible="False" SortExpression="RollCallID"
Visible="False">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("RollCallID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StudentID" SortExpression="StudentID" Visible="False">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("StudentID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student" SortExpression="StudentName">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("StudentName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" SortExpression="CheckStatusName" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("CheckStatusName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RollCallPeriod0" Visible="False">
<ItemTemplate>
<asp:CheckBox ID="cbRollCallPeriod0" runat="server" />
<asp:HiddenField ID="hfRollCallPeriod0" runat="server" Value='<%# Eval("RollCallPeriod") %>' />
</ItemTemplate>
<HeaderStyle Font-Size="Small" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="RollCallPeriod1" Visible="False">
<ItemTemplate>
<asp:CheckBox ID="cbRollCallPeriod1" runat="server" />
<asp:HiddenField ID="hfRollCallPeriod1" runat="server" Value='<%# Eval("RollCallPeriod") %>' />
</ItemTemplate>
<HeaderStyle Font-Size="Small" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
..
etc with the RollCallPeriod2, 3, 4 and so on, as many as required.
然后我会在 Page_Load()
上打开它们,具体取决于我需要的一天的时间。
DataSourceSelectArguments args = new DataSourceSelectArguments();
DataView dv = sdsRollCallPeriods.Select(args) as DataView;
DataTable dt = dv.ToTable() as DataTable;
if (dt != null)
{
int wsCol = 4; //start of PeriodTimes column in gvLocationBoard
int wsPos = 0;
foreach (DataRow dr in dt.Rows)
{
gvLocationBoard.Columns[wsCol + wsPos].HeaderText = dr.ItemArray[1].ToString();
gvLocationBoard.Columns[wsCol + wsPos].Visible = !gvLocationBoard.Columns[wsCol + wsPos].HeaderText.StartsWith("RollCallPeriod");
wsPos += 1;
}
}
瞧,我有我的行和我的动态列!