views:

57

answers:

3

Hello,

I have some experience in developing business logic and database logic and primarily work with C#. Now I would like to build my first data-driven presentation-tier in ASP.NET. I was just wondering how I should fill my DataGrids with data.

I have IList - Collections of business entities such as "Customer" which I retrieve from my BLL. It is easy to bind the whole collection to a control, but that means, that all public properties of my business entities are shown as columns. I cannot decide which properties to show, how they should be ordered and how they should be displayed (e.g. DateTime as ShortTimeString).

Could you please tell me where to find best-practise examples or tell me how you solved this problem?

Best regards, Daniel Lang

A: 

I use a "dirty" sort of trick to format my binding on gridviews and the like. Once you add your control, you can (in the designer) select its datasource. I typically choose ObjectDataSource and on the next wizard screen uncheck the box for showing only data objects. Then I find the function in my BLL that I intend to bind to (or a method that returns the same class/datasource) and select it. I blaze through the wizard as if I'm really binding to that method, and when it's done, the control is formatted for a 2-way bind to that source.

After that, I delete the objectdatasource object in the form an set the control's datasource to "NONE". When it prompts to reconfigure the control, I just click no. The control is now formatted and unbound. From here (for GridViews) you can go into Edit Columns and reposition them with the designer, format them, apply styles, adjust bindings, etc.

As a best practice however, I bind to function returns rather than a true 2-way binding. We have specific BLL/DAL objects for handling those operations so I use a method to retrieve data and then we use a method to put data in.

Joel Etherton
+2  A: 

Whenever I bind to grid I make sure to always have the property AutoGenerateColumns = false. I then explicitly define the columns and templates I use.

I think this is best practice since the next coder might come along and add something to your object and not realize it is going to automatically be added to everywhere you bind it to a grid. It also makes you consider each field to see if they really need to see it as is or with some type of template.

The AutoGenerateColumns property is one of those features that looks really cool in demos but in the real world is not that useful and can lead to unexpected issues.

Kelsey
Thank you for your suggestions - this looks like being the "real" best-practise!
dlang
A: 

As was stated by @Kelsey you should set the AutoGenerateColumns = false and manually define the columns. However, it seems that you know you should only show certain information, but you don't know which pieces to show and in what format.

I cannot decide which properties to show, how they should be ordered and how they should be displayed (e.g. DateTime as ShortTimeString).

Could you please tell me where to find best-practise examples or tell me how you solved this problem?

This really depends on what is most useful for the user of your application. Ask yourself some questions. Better yet ask these questions to someone who will use it other than yourself.

Some thoughts:

  • What would a user be interested in seeing?
  • Don't just show data to show it
  • It should serve some purpose to the user
  • DateTime format - does the user need to see more than a ShortTimeString? Would knowing the day help them? Maybe show the long string in a tooltip and show the short to save space.
David Glass
Thank you for your help!As you might have recognized my english is not that good - I actually meant that I don't know how I could achieve that some properties are not shown.BTW, your tips were helpful and I will consider them in my work!
dlang