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; }
}
}
}
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; }
}
}
}