What most answers here don't explain is - what if you need to make columns visible again and invisible, all based on data dynamically? After all, shouldn't GridViews
be data centric?
What if you want to turn ON or OFF columns based on your data?
My Gridview
<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" OnPreRender="gvLocationBoard_PreRender">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<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:HiddenField ID="hfStatusID" runat="server" Value='<%# Eval("CheckStatusID") %>' />
<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..
Note the `"RollCallPeriodn", where 'n' is a sequential number.
The way I do it, is to by design hide all columns that I know are going to be ON (visible="true") or OFF (visible="false") later, and depending on my data.
In my case I want to display Period Times up to a certain column. So for example, if today is 9am then I want to show periods 6am, 7am, 8am and 9am, but not 10am, 11am, etc.
On other days I want to show ALL the times. And so on.
So how do we do this?
Why not use PreRender
to "reset" the Gridview
?
protected void gvLocationBoard_PreRender(object sender, EventArgs e)
{
GridView gv = (GridView)sender;
int wsPos = 3;
for (int wsCol = 0; wsCol < 19; wsCol++)
{
gv.Columns[wsCol + wsPos].HeaderText = "RollCallPeriod" + wsCol.ToString("{0,00}");
gv.Columns[wsCol + wsPos].Visible = false;
}
}
Now turn ON the columns you need based on finding the Start of the HeaderText and make the column visible if the header text is not the default.
protected void gvLocationBoard_DataBound(object sender, EventArgs e)
{
//Show the headers for the Period Times directly from sdsRollCallPeriods
DataSourceSelectArguments dss = new DataSourceSelectArguments();
DataView dv = sdsRollCallPeriods.Select(dss) as DataView;
DataTable dt = dv.ToTable() as DataTable;
if (dt != null)
{
int wsPos = 0;
int wsCol = 3; //start of PeriodTimes column in gvLocationBoard
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;
}
}
}
I won't reveal the SqlDataSource
here, but suffice to say with the PreRender
, I can reset my GridView
and turn ON the columns I want with the headers I want.
So the way it works is that everytime you select a different date or time periods to display as headers, it resets the GridView to the default header text and Visible="false" status before it builds the gridview
again. Otherwise, without the PreRender
, the GridView will have the previous data's headers as the code behind wipes the default settings.