I have a webGrid on as .asp page which I want to populate from a datatable at run time and I want it to automatically generate the column headings.
When it tries to display the page, it throws an unhandled exception. See Stack Trace below.
Also below I have attached a snippet of my .asp code.
If I set the datasource then call .CreateAutoGenratedColumns() then call .DataBind() it throws the error.
If I set the datasource then create a BindColumn and add it to the WebWorksheet, then call .DataBind() it works (but this isn't what I want. I do not know the columns ahead of time, they are whatever is returned to me from my stored procedure call)
Stack Trace:
|
ASP
<asp:Panel ID="PanelGridWeb" runat="server">
<acw:GridWeb ID="gwResults" runat="server">
</acw:GridWeb>
</asp:Panel>
<br />
<asp:Panel ID="PanelGridView" runat="server">
<asp:GridView ID="gvResults" runat="server" CssClass="gvResults"
ShowHeaderWhenEmpty="True" onrowdatabound="gvResults_RowDataBound">
<AlternatingRowStyle BackColor="White" Wrap="False" />
<HeaderStyle BackColor="#CCCCCC" />
<RowStyle Wrap="False" />
<SelectedRowStyle BackColor="#CCCCCC" />
</asp:GridView>
</asp:Panel>
<br />
-------------------
Code Behind
WebWorksheet sheet = gwResults.WebWorksheets[0];
sheet.Cells.Clear();
sheet.EnableCreateBindColumnHeader = true;
sheet.BindStartRow = 1;
sheet.DataSource = dt;
// this fails
sheet.CreateAutoGenratedColumns();
//// this works (hard-coded bound columns)
//BindColumn boundCol1 = new BindColumn();
//boundCol1.DataField = "Case ID";
//boundCol1.Caption = "Case ID";
//sheet.BindColumns.Add(boundCol1);
sheet.DataBind();