Need help creating list of business objects

M

moondaddy

I'm new to c# and .net 2.0. In the old vb.net 1.1 days I normally created a
list class for every business class and used this list class for all
databinding rather than using datasets. This is because often I wanted to
edit data in the list and therefore needed to talk to the business object
and not the dataset. Since this was a must, I standardized on creating list
objects in most cases so everything was standardized. I've searched far and
wide and have found little documentation on the latest and greatest way to
create bindable custom list classes. What is the best practice these day in
creating bindable list classes? Where's good documentation about this?

Thanks.
 
M

moondaddy

Thanks. That was a useful link. However, when I try it, I'm getting an
error in every cell of the gridview and haven't identified why yet. Also, I
usually have added functionality in my list classes. Is there a code
example of inheriting the BindingList class and creating a new list class
with it? I tried it but the controls and/or the designer could not see the
fields in the objects in the list.

Thanks again.
 
J

Jeffrey Tan[MSFT]

Hi moondaddy,

Thanks for your feedback.

Yes, I have downloaded your sample project and reproduced out this
behavior.

Actually, the cell error is displayed by IDataErrorInfo interface. When
DataGridView shows the cells, it will query IDataErrorInfo interface and
display the error message. If you return an empty error message for
IDataErrorInfo.Error and IDataErrorInfo.this[] property, the cell error
icon will disappear. Like below:

#region IDataErrorInfo Members

public string Error
{
get { return ""; }
//return "some error stub";
}

public string this[string columnName]
{
get { return ""; }
}

#endregion


Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
M

moondaddy

Thanks Jeffrey! Question. If I return an empty string, then what logic do
I use to know when to return an error. Is this based on my business logic
where I would set a private variable to some error message that this would
pick up (there must be a better way to pass a error msg to this event)? How
would I know when to pass an error msg for one specific cell and not all of
them?
 
W

William Stacey [MVP]

It would be cool if you could do:

public class Customer
{
public string Name;
public ComboBox ShoeSize;
public ProgressBar DownloadProgress;
}

And do a binding list to grid or listview and have it display the combo box
and progress bar in the column automatically.
--
William Stacey [MVP]

| Hi moondaddy
| Here you can find a usefull summary of creating list of business
| objects for binding using BindlingList class of .Net Framework 2.0 ( a
| cool feature !) :
|
| http://blogs.msdn.com/dchandnani/archive/2005/03/12/394438.aspx
|
| you can search the web for BindingList class, too.
| I hope this helps
|
| A.Hadi
|
 
J

Jeffrey Tan[MSFT]

Hi moondaddy,

Thanks for your feedback.

We can create a SetObjectError() method in Person_BLL class, which sets
"Error" and "this[string columnName]" property error string. Then for
Person_BLL class usage developer, he can use SetObjectError() to set the
error information.

In FCL, DataRowView class implements IDataErrorInfo interface, and DataRow
exposes SetColumnError method to set the error.

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
M

moondaddy

Thanks. I see plenty of documentation about SetColumnError in regards to
datasets and datarows, however, I doesn't really help me get my arms around
how to use a SetObjectError method in a business object. Can you recommend
some documentation or code examples? I found an example by Rocky Lhotka
Feb. 2003 which used old vb code. That example shows a red error 'dot' for
each grid row and not each cell. This way he could check the validity of
all properties in the Error property and return a string showing all errors.
What I don't understand is how to return a specific error for a specific
cell.
 
J

Jeffrey Tan[MSFT]

Hi moondaddy,

Sorry for the late response, I am OOF these days.

I can not find any definite sample regarding IDataErrorInfo. Maybe you can
search IDataErrorInfo in google for more information.

Actually, I did not see much difficulty in implementing this interface. As
I original said, IDataErrorInfo.Item property returns the specific cell
error message. In your implementation, one instance of Person_BLL is
expressed as a row in DataGridView. So, every column in this object
instance corresponding to one cell in that row. In this object, you can use
an Hashtable to store the error variable, which one item in the Hashtable
stores one column's error message.(we can use the columnName as the key)

In Person_BLL.SetObjectError method, we can pass the columnName as the
parameter, which can be used in this method to identify the error message
in Hashtable.(Yes, with the key(columName), we can find the item without
any problem)

Initially, all the items will be empty strings, so there will be no error
in DataGridView. Once, an item is non-empty string, it will express as an
error in corresponding cell.

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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

Top