Binding Bug Using Explicit Interface Implementation In C#.

B

Brandon Driesen

The following illustrates my question. Why is it when I bind to an a
collection of items whose interface implementation is explicit, there is
an error during the binding process wherein the error is as follows:

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

Seems to me there is a shortcoming of the Binding Process to Web
Controls. There should not be any impediment doing what I am doing since
there can be legitimate reasons for using explicit interface implementation.



ASPX Page

<asp:GridView ID="gvMain" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
</Columns>
</asp:GridView>

Code Behind

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace Research
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
List<IReadOnlyUserData> users = new
List<IReadOnlyUserData>();
users.Add(new User("John", "Doe"));
users.Add(new User("Jane", "Doe"));
users.Add(new User("Joe", "Bloggs"));
this.gvMain.DataSource = users;
this.gvMain.DataBind();
}
}
}

public interface IReadOnlyUserData
{
string FirstName { get; }
string LastName { get; }
}

public class User : IReadOnlyUserData
{
private string _givenNames;
private string _surname;

public User() { }

public User(string givenNames, string surname)
{
this._givenNames = givenNames;
this._surname = surname;
}

public string GivenNames
{
get { return this._givenNames; }
set { this._givenNames = value; }
}

public string Surname
{
get { return this._surname; }
set { this._surname = value; }
}



string IReadOnlyUserData.FirstName
{
get { return this._givenNames; }
}

string IReadOnlyUserData.LastName
{
get { return this._surname; }
}


}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top