none
Is there any way to DataBind to a DataTable where the binding will recognize public properties rather than just defined columns? RRS feed

  • Question

  • I like using DataSets as an intermediate point between my application and database, but would prefer to have more flexibility when trying to "denormalize" certain columns.  I have used Expression columns in the past, to reference column values in other tables, but that gets old and is very limited in what it can do.  I'd like to be able to add a new public property to the <table>Row class and have that property show up in the grid, but I haven't been able to make this happen.

    Example: Employee (employeeID, name) & Department(departmentID, name, managerID) with a relation from Employee(employeeID) to Department(managerID).  I'd like to be able to do something like adding to DepartmentRow a public string ManagerName that references the DepartmentRow's EmployeeRow.Name, and have this show up in DataBinding.


    • Moved by Reed Copsey, JrMVP Monday, June 13, 2011 4:09 PM ADO.NET DataSet/DataTable question (From:.NET Base Class Library)
    Thursday, June 9, 2011 7:51 PM

Answers

  • I personally don't know of a way to make a DataSet/DataTable to show more than just the fields that came from the database server, so my answer would be it is not possible.  I must warn you, though, that I haven't used a DataSet for data binding in a long time as I'd rather bind to my own custom collections.
    MCP
    • Marked as answer by Matt Knowles Friday, June 10, 2011 12:23 PM
    Friday, June 10, 2011 4:58 AM

All replies

  • What you describe exactly is a middle tier.  Kuddos on pursuing it. :)

    What I recommend that you do for every database/client application that you do:  Create a "middle" class library project (DLL) that provides specialized classes that represent the objects stored in the database.  These classes are usually referred to as the business tier or middle tier.

    The mechanics

    Your middle tier will own the code to connect to the database and run all queries (Select, Update, Delete, and possibly others).  Your classes would have factory methods or getters that would query or add to the database as appropriate.  These factories and getters are what your application (the UI tier, or the external tier) would use to get the different objects.  If you return more than one object, like a search result probably would, you return a collection of those objects.  Databound controls accept as data source any class that implements IList.

    If you would like some design-time support for your data binding, you can enclose your collections inside BindingSource components.  I personally don't use them, so I cannot guide you much in this regard.

    Anyway, back to the topic:  When you databind to a collection of your own classes, the columns displayed are the properties of the objects inside the collection, which is what you are going for.


    MCP
    Thursday, June 9, 2011 8:23 PM
  • Thanks for the reply, but you didn't really answer my question.

    I'm really trying to get the best of both worlds here.  I want a DataSet in my client application that has all the normalized data and the extreme ease-of-use when databinding, but I'd like to be able to add custom non-DataColumn properties to my Typed DataRows so that those will show up in the grid as well when I data bind.

    My question: Is it possible to DataBind to non-DataColumn properties of a DataRow/DataTable, or will the binding *always* just look at the DataColumns once it recognizes it's binding to a DataRow/DataTable?

    Friday, June 10, 2011 3:19 AM
  • I personally don't know of a way to make a DataSet/DataTable to show more than just the fields that came from the database server, so my answer would be it is not possible.  I must warn you, though, that I haven't used a DataSet for data binding in a long time as I'd rather bind to my own custom collections.
    MCP
    • Marked as answer by Matt Knowles Friday, June 10, 2011 12:23 PM
    Friday, June 10, 2011 4:58 AM
  • I guess I'm having a hard time letting go of DataSets because I already have so much experience using them.  I'll take another look at using custom objects, trying to keep an open mind.

    Thanks again for the replies.

    Friday, June 10, 2011 12:25 PM