GridView:如何基于来自单独数据源的数据添加列 [英] GridView: how to add columns based on data from a separate data source

查看:233
本文介绍了GridView:如何基于来自单独数据源的数据添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以编程方式向列表添加列是很简单的。



有些示例



权限和时间,作为标题,是我想要根据 sdsRollCallPeriods SqlDataSource添加的新列。

解决方案

这不是一个真正的解决方案,而是一种解决方法,它适用于我。



我添加了我需要的列,并将它们全部设置为 Visible =false

 < ASP:GridView的ID = gvLocationBoard RUNAT = 服务器 AllowPaging = 真 AllowSorting = 真 ShowFooter = 假 ShowHeader = 真 可见= 真 的AutoGenerateColumns = 假 CELLPADDING = 4ForeColor =#333333GridLines =无
DataSour ceID =sdsLocationBoardOnDataBound =gvLocationBoard_DataBoundOnRowDataBound =gvLocationBoard_RowDataBoundPageSize =15>
< RowStyle BackColor =#F7F6F3ForeColor =#333333/>
<列>
< asp:TemplateField HeaderText =RollCallIDInsertVisible =FalseSortExpression =RollCallID
Visible =False>
< ItemTemplate>
< asp:Label ID =Label1runat =serverText ='<%#Eval(RollCallID)%>'>< / asp:Label>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =StudentIDSortExpression =StudentIDVisible =False>
< ItemTemplate>
< asp:Label ID =Label2runat =serverText ='<%#Eval(StudentID)%>'>< / asp:Label>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =StudentSortExpression =StudentName>
< ItemTemplate>
< asp:Label ID =Label3runat =serverText ='<%#Eval(StudentName)%>'>< / asp:Label>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =StatusSortExpression =CheckStatusNameItemStyle-Horizo​​ntalAlign =Center>
< ItemTemplate>
< asp:Label ID =Label4runat =serverText ='<%#Eval(CheckStatusName)%>'>< / asp:Label>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =RollCallPeriod0Visible =False>
< ItemTemplate>
< asp:CheckBox ID =cbRollCallPeriod0runat =server/>
< asp:HiddenField ID =hfRollCallPeriod0runat =serverValue ='<%#Eval(RollCallPeriod)%>'/>
< / ItemTemplate>
< HeaderStyle Font-Size =Small/>
< ItemStyle Horizo​​ntalAlign =Center/>
< / asp:TemplateField>
< asp:TemplateField HeaderText =RollCallPeriod1Visible =False>
< ItemTemplate>
< asp:CheckBox ID =cbRollCallPeriod1runat =server/>
< asp:HiddenField ID =hfRollCallPeriod1runat =serverValue ='<%#Eval(RollCallPeriod)%>'/>
< / ItemTemplate>
< HeaderStyle Font-Size =Small/>
< ItemStyle Horizo​​ntalAlign =Center/>
< / asp:TemplateField>
..
等与RollCallPeriod2,3,4等等一样多。

然后我打开 Page_Load(),这取决于我需要的日子的价值。

  DataSourceSelectArguments args = new DataSourceSelectArguments(); 
DataView dv = sdsRollCallPeriods.Select(args)作为DataView;
DataTable dt = dv.ToTable()as DataTable;
if(dt!= null)
{
int wsCol = 4; //开始在gvLocationBoard中的PeriodTimes列
int wsPos = 0;
foreach(DataRow dr in dt.Rows)
{
gvLocationBoard.Columns [wsCol + wsPos] .HeaderText = dr.ItemArray [1] .ToString();
gvLocationBoard.Columns [wsCol + wsPos]。可见= gvLocationBoard.Columns [wsCol + wsPos] .HeaderText.StartsWith( RollCallPeriod)!;

wsPos + = 1;


瞧,我有我的行和我的动态专栏! / p>

Adding columns to a GridView programmatically is easy.

There are examples here and here.

Here is a similar question, but it's been answered the same way - to create a datatable and bind it to the GridView.

But won't that remove the original link to the main data source?

My GridView below already has a bind to the sqldatasource below it.

            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>

However, what if you're trying to add column headers to a gridview based on a data source that is totally separate (individual) to the main data source, for the overall GridView?

This is my "column" sql datasource...

<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>

What I want to end up with is a Gridview like this ...

The checkboxes on the right and the times, as headers, are the new columns I want to add based on the sdsRollCallPeriods SqlDataSource.

解决方案

Well it's not really a solution, but rather a work-around, and it works for me.

I've added the columns I needed and simply set them all to 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.

I then turn them on on Page_Load(), depending on the day's worth of times I require.

    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;
        }
    }

Voila I have my rows and my dynamic columns!

这篇关于GridView:如何基于来自单独数据源的数据添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆