 |
|
 |
using System; using System.Collections.Generic; using System.Text;
namespace SimpleDataBinding { [AttributeUsage(AttributeTargets.All)] public class TypedCollectionAttribute : Attribute { private Type UnderlyingType;
public TypedCollectionAttribute(Type underlyingType) { UnderlyingType = underlyingType; }
public Type CollectionType { get { return UnderlyingType; } } } }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hello Geovanny,
The subject of your reaction triggered me. I want a to use a custom collection in a datagridview. But with all the capabilities of the datagridview when using a dataset. Like sorting for instance. To manage this all I read on programming forums is to implement the IBindingList interface. As mentioned in the article this is quite a hazzle. So i tried your solution, but as far as i can see the custom collection still cannot be sorted in the dgv. Am i missing something here? (Hopefully)? Or must i really implement Ibindinglist interface to make 'everything work'
Thanks in advance for your reply.
Kind regards.
Kees
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
.Net 2.0 makes this unnecessary?
Create two classes:
########################################################
using System; using System.Collections.Generic; using System.Text;
namespace SimpleDataBinding { public class Widget { private int id;
public int Id { get { return id; } set { id = value; } } private string name;
public string Name { get { return name; } set { name = value; } } } } ######################################################## using System; using System.Collections; using System.ComponentModel; using System.Globalization;
namespace SimpleDataBinding { [TypedCollection(typeof(Widget))] public class WidgetList : ArrayList{} }
#########################################################
Create a form:
#########################################################
Place DataGridView[dgvWidgets] and a Label [lblName]
########################################################
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;
namespace SimpleDataBinding { public partial class FrmTest : Form { public FrmTest() { init(); }
public void init() { InitializeComponent();
Widget wg1 = new Widget(); wg1.Id = 1; wg1.Name = "On";
Widget wg2 = new Widget(); wg2.Id = 2; wg2.Name = "Off";
WidgetList wgs = new WidgetList(); wgs.Add(wg1); wgs.Add(wg2); //Bind objects dgvWidgets.DataSource = wgs; lblName.DataBindings.Add("Text", wgs, "Name"); } } }
And everything works!!! Why go through all the trouble? Please correct me if I'm wrong.
Geovanny F. Fajardo Software Engineer
|
| Sign In·View Thread·PermaLink | 2.67/5 (3 votes) |
|
|
|
 |
|
|
 |
|
 |
Hi,
Thanks for a great article, just what I was looking for.
I have a couple of questions which I was hoping you might consider responding too, if you have time.
I was hoping your example code would show the use of events with the grid.
For example, I want to use your example to databind my objects to my grid and when data is entered into the objects from a backend data pump, the grid would update automaically to show the new data. I was hoping that by setting the IBindingList SupportChangeNotifications to true, that this would do the trick, but it did not, so I guess I must make some changes.
What I was hoping for was that your code would bring the same behaviour as when I databind a Dataset to a grid, when I do this the grid and the dataset automatically hookup and the grid refreshes when data is entered in the Dataset.
Can you suggest how this can be done using your code?
My second is one of usage. I read one person that wanted to have a single Collection to hold all business ojects. I was thinking more of modelling my business objects with a collection for each core business object type.
Would you be prepared to share your usage pattern with us?
Thanks again for the great work.
Steven Hawkes
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
how to make addnew row visible in datagrid? IBindingList.allownew return true why it's not visible?
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
Because there's a bug in the code. AddNew should not only create the object, but also add it to the collection. I lost a good hour on this one.
should be something like this:
object IBindingList.AddNew() { Object o = Activator.CreateInstance(type); Add(o); return o; }
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
 |
I don't know, I don't use the Microsoft supplied DataGrid. I use Devexpress controls. My Devex grid crashed until I realized that when it called AddNew(), it didn't just expect the object to be created, it also expected it to be added to the collection. Indeed, everybody else's implementation of AddNew() seems to do that.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi Dan,
This article really helped me a lot. I'd like to take my collections to the next level.
1. If my business object has some properties I don't want to display, how do I hide them?
2. The collection seems to accept any object (though it may not bind properly). How can I make it strongly typed?
3. It seems I need to build a new collection for each business object because the type is explicitly stated in the TypedCollection attribute. Is there a way around this so I can have one collection class to handle all my business objects? Perhaps a constructor parameter?
Thanks,
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
I made it strong typed by adding another constructor that takes the type: public TradeRequestCollection(Type listObjectType) { this.resetEvent = new ListChangedEventArgs(ListChangedType.Reset, -1); this.m_isSorted = false; this.m_listSortDirection = ListSortDirection.Ascending; this.m_sortProperty = null;
if (listObjectType == null) { throw new NullReferenceException("Must supply the type"); }
this.finalType = listObjectType; }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi Rob,
the strong typing was not the big problem, but thank you for your reply. That was my first meeting with the system.reflection namespace. I found another article, that fitted my requirements better.
http://www.codeproject.com/cs/database/BindArrayGrid.asp
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi,
I tried since a while to implement a custom generic collection which could be bound to a DataGrid, your example using IBindingList was very usefull for me to get the Arrows sorting on grid and the Find() method. 
I'm not very familiar with Interfaces, i tried to use the Find(PropertyDescriptor property,object key) Method from the IBindingList Interface. I had to cast the "MyCollection" Object into a (IBindingList) to access the Find Method and I'm asking myself if it is really necessary... and if there is a way to access directly to this Method trough my object or maybe should I implement a method that would wrap it all into it ?. Here is my code :
MyCollection myCollection = FakeData.GetData() ; // Creates a new collection of properties from MyCollection PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(t[0]);
// Sets an PropertyDescriptor to the specific property. System.ComponentModel.PropertyDescriptor myProperty = properties.Find("FirstName", true); int rowIndex = ((IBindingList)t).Find(myProperty, (object)"aaa0") ;
Thanx
Regards Faessler Gilles
Thanx
Regards Faessler Gilles
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I had been trying to bound textbox and combos to Collection that is not inhereted from CollectionBase, but implements IList, ICollection, IEnumerable and IEditableObject interfaces and inhereteds from Component, and I get no results, when I do a change on a value's property, the change isn't reflected on the control. Can you help me?
Thanks,
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
Any chance you could release the VB.NET version or even just mail it to me at crispin_at_caz.ltd.uk? I'm struggling with some aspects of conversion myself.
Thanks
Crispin
Crispin Horsfield Caz Limited, Bristol, UK +44 117 941 5920 www.caz.ltd.uk
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I'm a newbie to C#, but I know a bit about OOP via Java. I want to build my web application using good OOP architecture, and the strongly typed dataset just didn't turn me on. So I had these big plans to bind my classes/collections to web form components instead, and for three days I couldn't figure out how to get it to work!!!
Now I can do my thing and build my app the way I want. I've been agonizing over this for days, and there are discussions all over the map about how to implement OOP properly in C#.NET apps, with no answers, or just generalizations.
Maybe I'm just a slow, uninformed programmer newbie, but it seems to me that the majority of .NET web programmers will NOT have your code sample as an advantage and will be doing it the "easy" way, with datasets exposed in the UI!!!!! Encapsulation rules!
Thanks!!!!!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hey Dan,
your sample is the best implementation of the IBindingList interface I could find out there. Not even MS themselves were this complete in their example (thanks for the .Find method logic!)
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Have you tried to convert the MyCollection class to a component and access the data through the databinding picker? I modified it to implement IComponent, added it to the toolbox and dropped it onto a form. Then added a label and tried to use the databinding picker. It errored with 'Specified Method is not supported.' and wouldn't show the picker. Any ideas?
Thanks Bill
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Your article (and code) realy helped me understand DataBinding. Especially your SqlTypes solution worked out great for me. It is a pitty you didn't use it in your code sample (MyData.cs).
I also read this MSDN article about this subject.
bye, Fons
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |