Entity Framework Creates a Plural Table Name, But the View Expects a Singular Table Name

GridView bound with Properties of nested class

Only immediate properties of an instance can be displayed in a BoundField column.

One must instead use DataBinder.Eval in an itemtemplate to access the nested property instead of assigning it to a boundfield.

Example:

<asp:TemplateField>
<itemtemplate>
<p><%#DataBinder.Eval(Container.DataItem, "NestedClass.Name")%></p>
</itemtemplate>
</asp:TemplateField>

Alternatively, you can create a custom class which inherits BoundField and overrides GetValue to use DataBinder.Eval, as described in this blog post:

http://web.archive.org/web/20120121123301/http://iridescence.no/post/FixingBoundFieldSupportforCompositeObjects.aspx

How to map properties of inner/nested classes in DataGridView through BindingSource?

You can create a new class with all the properties that you want to be display in the grid and map it with your existing class either manually or using third-party libraries (ex. AutoMapper). Then bind the new class to Grid.

public class MyGridClass
{
public string FileName { get; set; }
public string Directory { get; set; }
public string Number { get; set; }
public string Prop1 { get; set; }
public string Prop2 { get; set; }
}

NesInfo ni = ...

MyGridClass gc = new MyGridClass ( );
gc.FileName = ni.FileName;
gc.Directory = ni.Directory;
gc.Number = ni.MapperInfo.Number;
gc.Prop1 = ni.MapperInfo.Prop1;
gc.Prop2 = ni.MapperInfo.Prop2;

C# DevExpress XtraGrid, bind to property of nested class

Lets assume you have the following classes in your code.

1) Address class

public class Address {
public string Street { get; set; }

public string City { get; set; }
}

2) User class

public class User {

public string UserName { get; set; }

public Address UserAddress { get; set; }
}

Now, that you want to bind a column Street to property User.Address.Street, this unfortunately wouldn't work by simply setting FieldName to "Address.Street"

But, if it is important that you accomplish it the way you want, I would suggest that you override the ToString() method of the Address class as follows:

public class Address {
public string Street { get; set; }

public string City { get; set; }

//Override ToString() method
public override string ToString() {
return this.Street;
}
}

Then, set the field name to "Address", instead of "Address.Street" which should do the trick.

Also another approach would be to add another readonly property called UserStreet in the User class:

public class User {

public string UserName { get; set; }

public Address UserAddress { get; set; }

public UserStreet {
get { return UserAddress != null ? UserAddress.Street : ""; }
}
}

And then set FieldName to "UserStreet".

Hope this helps.

asp.Net GridView bind custom object with nested List

I was able to solve this using a DataTable as your datasource for the Grid. I don't like the idea of moving from a nice clean object to a DataTable, but it provides support for the dynamic binding you need. I modified your friend object to have a few constructors. This allowed me to cleanup the static code declaration but might not be necessary in your implmentation.

The basic idea is that you will step through all possible friends, add their name as a DataColumn in a DataTable, then fill in the data for all person objects and their respective friends. This could probably be written to work in a single iteration of the allPerson object but I preferred two iterations to make the code easier to read.

The solution is written for c# 3.5 but could be converted for older versions by changing the static data declaration. I hope this helps.

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// setup your person object with static data for testing
List<person> allPerson = new List<person>()
{
new person()
{
name = "Dan",
age = 21,
allMyFriends = new List<friend>() { new friend("James"), new friend("John"), new friend("Matt") }
},
new person()
{
name = "James",
age = 21,
allMyFriends = new List<friend>() { new friend("Dan"), new friend("Matt"), new friend("Tom") }
},
new person()
{
name = "John",
age = 21,
allMyFriends = new List<friend>() { new friend("Dan") }
},
new person()
{
name = "Matt",
age = 21,
allMyFriends = new List<friend>() { new friend("Dan"), new friend("James") }
},
new person()
{
name = "Tom",
age = 21,
allMyFriends = new List<friend>() { new friend("James") }
}
};

System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Age");

foreach (person p in allPerson)
{
// step through each person and look at their friends
foreach (friend f in p.allMyFriends)
{
// look to see if this friend has a column already
if (!dt.Columns.Contains(f.name))
{
dt.Columns.Add(f.name);
}
}
}

foreach (person p in allPerson)
{
// create the datarow that represents the person
System.Data.DataRow dr = dt.NewRow();
dr["Name"] = p.name;
dr["Age"] = p.age;

// find the friends and mark them
foreach (friend f in p.allMyFriends)
{
dr[f.name] = "X";
}

dt.Rows.Add(dr);
}

// fill the list
this.Grid.DataSource = dt;
this.Grid.DataBind();

}
}

public class person
{
public string name;
public int age;
public List<friend> allMyFriends = new List<friend>();
}

public class friend
{
public string name;
public string address;

public friend()
{

}

public friend(string name)
{
this.name = name;
}

public friend(string name, string address)
{
this.name = name;
this.address = address;
}
}

Edit:
I forgot to add how this is rendered.

-------------------------------------------------
| Name | Age | James | John | Matt | Dan | Tom |
-------------------------------------------------
| Dan | 21 | X | X | X | | |
| James | 21 | | | X | X | X |
| John | 21 | | | | X | |
| Matt | 21 | X | | | X | |
| Tom | 21 | X | | | | |
-------------------------------------------------

Binding Complex Data source of a class into Grid View

There is no built-in support for Master Details in the DataGridView Control in Windows Forms. But we have the option to use Third Party Libraries Like:

  • Telerik.
  • Devexpress.

As for the binding issue, yes you need to have a public property with a public getter:

public List<Shops> details { set; get; } = new List<Shops>();

For prior to C# 6 you can use:

public class CustomerDebt
{
public CustomerDebt()
{
details = new List<Shops>();
}

public List<Shops> details { set; get; }
}


Related Topics



Leave a reply



Submit