Search code examples
gridviewdata-bindingdatatableshowgridviewcolumn

GridView without data won't show columns


i can't seem to get the columns to show up on the page. I want to show the gridview with the columns, but without any data. How can i do that?

**Presenter.cs**
public DataTable GetAllSelectedColumns(int onderzoekId)
    {
        DataTable dt = new DataTable();
        var onderzoek = OnderzoekModel.GetOnderzoek(onderzoekId);

        foreach (var cn in OnderzoekColumnModel.GetSelectedColumns(onderzoek.OnderzoekId))
        {
            dt.Columns.Add(cn.ColumnName);
        }

        return dt;
    }

**Page.aspx**

<asp:Panel runat="server" ID="pnlContainer" CssClass="onderzoek-data">
                <asp:GridView ID="mainGridViewFixedColumns" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="True">
                </asp:GridView>
                <asp:GridView ID="mainGridView" runat="server" Font-Size="Small">
                    <HeaderStyle CssClass="GVHeader" />
                    <FooterStyle CssClass="GVFooter" />
                </asp:GridView>
            </asp:Panel>

**Codebehind**
public void DisplayFixedColumns(DataTable data)
    {
        mainGridViewFixedColumns.DataSource = data;
        mainGridViewFixedColumns.DataBind();
    }

Solution

  • The solution from hutchonoid did the trick. I've tried that before but crazily now it works!

    I have a populated gridview and a non-populated gridview now. How will i dynamically get the same styling on both headers?