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.