Formatting/Display in a DataGrid

  • Thread starter Thread starter Stephen
  • Start date Start date
S

Stephen

Hi,

Is there anyway I can make a column thats databound into a hyperlink to
navigate to another page?
Suppose I have 3 columns: SortOrder, Description, UserName

for eg:
<Columns>
<asp:HyperLinkColumn Text="View" DataNavigateUrlField="UserId"
DataNavigateUrlFormatString="ViewInfo.aspx?id={0}">
<HeaderStyle BackColor="#CCCCCC"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:HyperLinkColumn>
<asp:BoundColumn DataField="SortOrder" HeaderText="Sort Order"
SortExpression="SortOrder">
<ItemStyle HorizontalAlign="Center" Width="12%"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Description" HeaderText="Description">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="UserName" HeaderText="UserName"
SortExpression="UserName">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
</Columns>


Instead of having HyperLinkColumn with "View" as text, how can I make
"Description" or whatever column I want
into the hyperlink (with the relevant info to display) and navigate it to
"ViewInfo.aspx?id={0}


please Advice,
Stephen
 
using templates :-)

templates give you all teh free in the world to add something into the cell
in a row...

after the last </asp:BoundColumn>
add this for example: (just copy/paste)

<asp:TemplateField>
<ItemTemplate>
<%#Eval("Description")%> <br />
<asp:HyperLink runat="server" id="myLink" text="more..."
NavigateUrl='<%# Eval("UserId", "ViewInfo.aspx?id={0}") %>' />
</ItemTemplate>
</asp:TemplateField>

and then tell me the result ;-)
 
Hi Bruno,

Thanks for the info but..... I tried your code suggestion but it still gives
me this error:

<Columns>
<asp:HyperLinkColumn Text="View" DataNavigateUrlField="UserId"
DataNavigateUrlFormatString="ViewInfo.aspx?id={0}">
<HeaderStyle BackColor="#CCCCCC"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:HyperLinkColumn>
<asp:BoundColumn DataField="SortOrder" HeaderText="Sort Order"
SortExpression="SortOrder">
<ItemStyle HorizontalAlign="Center" Width="12%"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Description" HeaderText="Description">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="UserName" HeaderText="UserName"
SortExpression="UserName">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:Templatecolumn>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Description") %>
<br />
<asp:HyperLink runat="server" id="myLink" text="more..." NavigateUrl='<%#
DataBinder.Eval("Ruleid", "ViewInfo.aspx?id={0}") %>' />
</ItemTemplate>
</asp:Templatecolumn>
</Columns>


gives me this error:

DataBinder.Eval: 'System.String' does not contain a property with the name
ViewInfo.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Web.HttpException: DataBinder.Eval:
'System.String' does not contain a property with the name ViewInfo.

Source Error:


<%# DataBinder.Eval(Container.DataItem, "Description") %>
<br />
<asp:HyperLink runat="server" id="myLink" text="more..." NavigateUrl='<%#
DataBinder.Eval("Ruleid", "ViewInfo.aspx?id={0}") %>' />
</ItemTemplate>
</asp:Templatecolumn>
 
remove the DataBinder... just Eval("name") like my example

or Eval("string{0}", "name")

or the same using Bind("name") if you nees to pass the value!

it's the way to do in ASP.NET 2.0
 
Thanks for the reply... I am using v1.x
anyways I tried it this way and it worked but would welcome suggestion from
you too

<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Description") %>
<br />
<asp:HyperLink runat="server" id="myLink" text="more..." NavigateUrl='<%#
"ViewInfo.aspx.aspx?id=" & Container.DataItem("UserId") %>' />
</ItemTemplate>

Thanks,
Stephen
 
I was using 2.0 - I passed directly from classic asp to asp.net 2.0, so I
never did anything in 1.x ...

sorry,

but if it works... better :-)

I just love 2.0 version ;-) and for that GridView is really nicer to work
than the old datagrid (seeing all the examples I see in both controls)

you can use the template to show normal rows (itens), the update row
(EditItem), the select row, etc...
check for templateItem under datagrid in your Help file
 
Back
Top