views:

29

answers:

2

I know that you can use exclamation sign to bind array of simple types (like string) to GridView like this

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
       <asp:BoundField HeaderText="Array Field" DataField="!" />
    </Columns>    
</asp:GridView>

But this doesn't seem to be the case with DataNavigateUrlFields

<asp:HyperLinkField DataNavigateUrlFields="!" DataNavigateUrlFormatString="RoleInformation.aspx?role={0}" Text="Manage users" />

and I get following error:

A field or property with the name '!' was not found on the selected data source.

A: 

Most people probably haven't even know to use the ! field I suspect. When I read your question it actually made me remember that feature which I had read about but never actually used. With that in mind, I don't think there is a way with that type of field because it was probably forgotten in the HyperLinkField implementation (just a guess). You could just do a quick conversion to named property and then you don't have any issues:

Example:

<asp:GridView ID="grdTest" runat="server" AutoGenerateColumns="false"> 
    <Columns> 
        <asp:BoundField HeaderText="Array Field" DataField="data" />
        <asp:HyperLinkField DataNavigateUrlFields="data" DataNavigateUrlFormatString="RoleInformation.aspx?role={0}" Text="Manage users" /> 
    </Columns>     
</asp:GridView>

Notice the field named data. Then to bind your array just do:

string[] testArray = { "1", "2", "3" };
grdTest.DataSource = testArray.Select(a => new { data = a });
grdTest.DataBind();

It doesn't replace the ! directly but it is a simple solution to get around binding to simple arrays that will always work even when the ! isn't implemented which it probably needs to be for each field type.

Kelsey
That is almost what I did for now
Sergej Andrejev
@Sergej Andrejev I don't think there really is another way. If you do find a way to get teh `!` working leave a comment but I just don't think it was implemented.
Kelsey
A: 

That DataField="!" option seem to be for GridView only "when you want to use the GridView to display a collection of "primitive" types whose values aren't contained in properties"(MSDN Comm Content) such as arrays, IEneumerables etc. It then ties each value from the collection to the BoundField. For HyperLinkField's DataNavigateUrlFields property (string[]), it doesn't seem to implement this idea.

MSI