aspx_beginners thread: Re: Setting DataGrid column values from code, not in <asp> tag?
I finally found a way to solve your (first) problem.
Because the HyperLinkColumn object doesn't exist when parsing the page,
you have to reference it in another way. Here is an example in C# and VB
C#:
HyperLinkColumn hlcTemp;
hlcTemp = checked((HyperLinkColumn)list.Columns[0]);
VB:
Dim hlcTemp as HyperLinkColumn;
hlcTemp = CType(list.Columns[0], HyperLinkColumn);
I did not check the VB code, but it should work.
The trick is to explicit convert the first column of the datagrid to the
hyperlinkcolumn type and reference that.
Then it is possible to acces all HyperLinkColumn attributes.
Good luck,
Ivo
> Most <asp: controls have an id you can set, which you can then use in
your
> code to set all the other properties.
> i.e. <textarea id="thisText" runat="server" />
>
> thisText.Value = "Hello there";
> thisText.Rows = 20;
> thisText.Cols = 40;
> etc...
>
> is there a way to set the properties of the HyperLinkColumn and/or
> BoundColumn for a DataGrid from the code?
> the datagrid below works, but i would like to beable to set the
> HyperLinkColumn properties like this
>
> list.HyperLinkColumn.DataNavigateUrlField = "ID";
> list.HyperLinkColumn.DataNavigateUrlFormatString = "form.aspx?
> action=Edit&ID={0}";
> list.HyperLinkColumn.DataTextField = "Title";
>
> then the diplay would be completly removed from the logic
> <asp:HyperLinkColumn /> of course this is where an id property would
come
> in handy.
>
>
> instead of like this -
> <asp:DataGrid
> id="list"
> AutoGenerateColumns="false"
> ShowHeader="false"
> runat="server">
> <Columns>
> <asp:BoundColumn DataField="ID" Visible="false" />
> <asp:BoundColumn DataField="Title" Visible="false" />
>
> <asp:TemplateColumn>
> <itemtemplate>
> <b> - </b>
> </itemtemplate>
> </asp:TemplateColumn>
>
> <asp:HyperLinkColumn
> DataNavigateUrlField="ID"
> DataNavigateUrlFormatString="form.aspx?action=Edit&ID={0}"
> DataTextField="Title" />
>
> </Columns>
> </asp:DataGrid>
>
>
> on another note is it possible to insert comments in a datagrid
> this seems to throw an erorr
> <asp:DataGrid id="list" runat="server" />
>
> <Columns>
> <!-- Hidden Data --->
> <asp:BoundColumn DataField="ID" Visible="false" />
> <asp:BoundColumn DataField="Title" Visible="false" />
>
> <!-- for formatting only -->
> <asp:TemplateColumn>
> <itemtemplate>
> <b> - </b>
> </itemtemplate>
> </asp:TemplateColumn>
>
> <!-- Link to Edit Form -->
> <asp:HyperLinkColumn
> DataNavigateUrlField="ID"
> DataNavigateUrlFormatString="form.aspx?action=Edit&ID={0}"
> DataTextField="Title" />
>
>
> </asp:DataGrid>