It all depends. Using complex objects like the NameValue requires a little more code than with simple objects. However, the principle stays the same.
Here's a sample GridView template. It uses a Bug class with a Status property that is of type NameValue:
Code:
<asp:TemplateField HeaderText="Status" SortExpression="Status">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
SelectedValue='<%# CType(Eval("Status"), NameValue).Value %>'>
<asp:ListItem Value="33">Item 33</asp:ListItem>
<asp:ListItem Value="1">Item 1</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# CType(Eval("Status"), NameValue).Name %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
As you can see, I am using Eval instead of Bind to display the Name in the ItemTemplate and I use CType(Eval("Status"), NameValue).Value to preselect an item in the dropdown.
Next, I have the following code in the Updating event of the DropDown, almost identical to the code I posted earlier, except for the fact I am creating a new NameValue instance with a fake name:
Code:
Dim myList As DropDownList = _
CType(GridView1.Rows(e.RowIndex).FindControl("DropDownList1"), DropDownList)
e.NewValues("Status") = New NameValue("Fake Name", myList.SelectedValue)
When finally the UpdateItem method is called, it accepts an instance of Bug with its Status set to the NameValue created in the code above.
As you can see, it requires more work than simple properties. However, once you understand how it works, it's much easier to apply it, and keep using complex objects like NameValue.
Hopefully, this post helps you understand how to do databinding with complex objects...
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of
ASP.NET 2.0 Instant Results and
Beginning Dreamweaver MX / MX 2004
While typing this post, I was listening to:
Future Proof by
Massive Attack (Track 1 from the album:
100th Window)
What's This?