Home All Groups Group Topic Archive Search About

Dynamically adding DataGrid columns

Author
8 Feb 2005 10:59 AM
Ian Oldbury
Hi,

Does anyone know how to add columns dynamically to a DataGrid.

i populate the grid using the .ItemDataBound

the current structure of my grids are as below....

<asp:templatecolumn>
<headertemplate>
  <asp:label id="lblDisp" runat="server" text="" tooltip="" />
</headertemplate>
<itemtemplate>
  <asp:label id="lblSales" runat="server" cssclass="GridTDText"></asp:label>
  <br />
  <asp:label id="lblProfit" runat="server"
cssclass="GridTDText"></asp:label>
  <br />
  <asp:label id="lblMargin" runat="server"
cssclass="GridTDText"></asp:label>
</itemtemplate>
<edititemtemplate>
  <asp:textbox id="txtSales" runat="server" cssclass="GridTextBox Small"
></asp:textbox>
  <br />
  <asp:textbox id="txtProfit" runat="server" cssclass="GridTextBox
Small"></asp:textbox>
  <br />
  <asp:textbox id="txtMargin" runat="server" cssclass="GridTextBox
Small"></asp:textbox>
</edititemtemplate>
</asp:templatecolumn>

Author
8 Feb 2005 2:19 PM
Alvin Bruney [MVP]
One approach is to simply add an extra column to your dataset and then bind
the dataset to the datagrid. The extra column would then show up
automatically.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://tinyurl.com/27cok
----------------------------------------------------------


Show quoteHide quote
"Ian Oldbury" <ian_oldbury@_NO_msn_SPAM_.com> wrote in message
news:%23ui3a3cDFHA.1260@TK2MSFTNGP12.phx.gbl...
> Hi,
>
> Does anyone know how to add columns dynamically to a DataGrid.
>
> i populate the grid using the .ItemDataBound
>
> the current structure of my grids are as below....
>
> <asp:templatecolumn>
> <headertemplate>
>  <asp:label id="lblDisp" runat="server" text="" tooltip="" />
> </headertemplate>
> <itemtemplate>
>  <asp:label id="lblSales" runat="server"
> cssclass="GridTDText"></asp:label>
>  <br />
>  <asp:label id="lblProfit" runat="server"
> cssclass="GridTDText"></asp:label>
>  <br />
>  <asp:label id="lblMargin" runat="server"
> cssclass="GridTDText"></asp:label>
> </itemtemplate>
> <edititemtemplate>
>  <asp:textbox id="txtSales" runat="server" cssclass="GridTextBox Small"
> ></asp:textbox>
>  <br />
>  <asp:textbox id="txtProfit" runat="server" cssclass="GridTextBox
> Small"></asp:textbox>
>  <br />
>  <asp:textbox id="txtMargin" runat="server" cssclass="GridTextBox
> Small"></asp:textbox>
> </edititemtemplate>
> </asp:templatecolumn>
>