 |
|
 |
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; }
}
}
}
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
.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
|
|
|
|
 |
|
 |
I just found the article that well explained the tricky ITypedList.GetItemProperties:
http://weblogs.asp.net/fbouma/articles/115837.aspx[^]
In short, when listAccessor is not null, it contains the path of hierarchical lists, that's why normally we only care about the last item.
Nice article.
- Weifen
-- modified at 0:02 Tuesday 9th May, 2006
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
Hi, how can I use your binding to use an XML file as data source?
Thanx
|
|
|
|
 |
|
 |
how to make addnew row visible in datagrid?
IBindingList.allownew return true
why it's not visible?
|
|
|
|
 |
|
 |
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;
}
|
|
|
|
 |
|
 |
It's not working,
What is the event that tells the datagrid to refresh and drow the new element?
Fessler
|
|
|
|
 |
|
 |
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.
|
|
|
|
 |
|
 |
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,
|
|
|
|
 |
|
 |
I think we have the same ideas. Ich would like to know that too. Any idea?
|
|
|
|
 |
|
 |
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;
}
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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,
|
|
|
|
 |
|
 |
Hi.
There is an example I wrote for data binding to combo boxes, to be found here:
http://www.olero.com/AspNetForums/ShowPost.aspx?PostID=38
I hope this helps.
Dan Glass
Olero Software
ORM.NET Data Access made easy
Personal site:
www.danglass.com
|
|
|
|
 |
|
 |
I converted it to vb.net and it still works .
Nice job done
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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!!!!!
|
|
|
|
 |
|
 |
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!)
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
That MSDN article is outdated and wrong...
|
|
|
|
 |