473,511 Members | 16,110 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to expose a collection as something I can set DataSource to for binding?

1 New Member
I have a simple Windows Form (C#, 3.5) with a DataViewGrid on it. My class also exposes a collection which is a list of the currently running processes.

I'd like to hook the DataViewGrid up so that it shows this list of processes, but setting .DataSource to my _currentlyActiveProcesses was not quite enough (nothing happened). AutoGenerateColumns is true on the DataViewGrid.

So my question is really in two parts:

1) What else do I need to do in order to bind a DataViewGrid to an enumerable list of objects and have it display them?

2) What do I need to do (attributes, perhaps?) in my class's collection show up as a valid data source in the Designer?

Expand|Select|Wrap|Line Numbers
  1.         public IEnumerable<Process> _currentlyActiveProcesses
  2.         {
  3.             get
  4.             {
  5.                 return System.Diagnostics.Process.GetProcesses().Where(process =>
  6.                 {
  7.                     bool hasException = false;
  8.                     try { IntPtr x = process.Handle; }
  9.                     catch { hasException = true; }
  10.                     return !hasException;
  11.                 });
  12.  
  13.             }
  14.         }
Oct 31 '08 #1
2 1607
Plater
7,872 Recognized Expert Expert
DataGridViews seem to use Property items as column headers with custom objects.
You could consider turning your collection into a DataTable?
Oct 31 '08 #2
mldisibio
190 Recognized Expert New Member
You are on the right track, but just a tad too abstract.

At a minimum, you should expose the collection as an IList, not IEnumerable.

Now, the DataGrid is pretty intuitive, and it might wire itself automatically to such an IList directly, but the more de-coupled design would be:

- Wrap your IList in a BindingList.
- Create a BindingSource whose DataSource is the BindingList.
- Set the DataGridView DataSource to the BindingSource.
Expand|Select|Wrap|Line Numbers
  1.   public IList<Process> _currentlyActiveProcesses
  2.   {
  3.       get
  4.       {
  5.           return System.Diagnostics.Process.GetProcesses().Where(pr  ocess =>
  6.           {
  7.               bool hasException = false;
  8.               try { IntPtr x = process.Handle; }
  9.               catch { hasException = true; }
  10.               return !hasException;
  11.           });
  12.  
  13.       }
  14.   }
  15.  
  16.   // Create a specialized list optimized for DataBinding:
  17.   BindingList bindingList = new BindingList(_currentlyActiveProcesses);
  18.  
  19.   // Create a BindingSource in order to follow a more decoupled DataBinding pattern.
  20.   // This is not necessary, but decouples your UI from the Data/Business layer:
  21.   // Also, set its DataSource in one step using the constructor:
  22.   BindingSource gridBinding = new BindingSource(bindingList, "");
  23.  
  24.   // Set the Grid's DataSource to the BindingSource:
  25.   myGrid.DataSource = gridBinding;
  26.  
Now you can handle events from the BindingList or BindingSource instead of the Grid.
The concept is that if you bound your list to a different control, most of your event
handling would not change.

Plater is right about the Grid using Properties to auto-wire columns. Therefore your
grid will now show the public Properties of the Process object as columns. You can create
custom columns and bind to specific properties if your need to.

Regarding your second question, I avoid using the designer for data binding, so someone else will have to answer the
steps needed to get your IList to appear as a DataSource in the designer.
Oct 31 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2873
by: GrumpyDev | last post by:
what is the best way to implement collection of custom entities?
2
2705
by: SammyBar | last post by:
Hi, I'm trying to bind a custom collection class to a data grid, following the guidelines from the article http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/default.aspx....
3
8963
by: Sefa Sevtekin | last post by:
I am trying to create a c# typesafe collection to avoid typecasting exceptions and to ease binding of grids to collections. I already did it in VB.NET but couldn't map it to C#. In VB.NET you need...
4
3972
by: Sefa Sevtekin | last post by:
I am trying to bind a strongly-typed collection to a data grid on a windows form like: BeerInventory inventory = new BeerInventory(); inventory.Add(new Beer ("Hamms", "Hamms Brewing Co.", 3));...
3
3834
by: charlie | last post by:
I have been messing around with data binding on Windows Forms, and I'm having a problem with the Property of a Collection (myCollection.Count) getting out of synch with label it's bound to. (My...
1
6169
by: Orlando Cavadini | last post by:
Hello I bind a collection of objects with Type AB (Class AB) to a DataGrid control as following: Public Class AB Private m_AA As Integer Private m_AB As Integer Private m_AC As Integer
2
1475
by: trebor | last post by:
I bind a collection to a data grid, and it works okay, but when I try to create customized columns, it just doesn't work. Here is the code. Note that if I uncomment that section below, and use "DT"...
3
2150
by: TS | last post by:
when binding to a datagrid for example, is it possible to use databinding syntax to bind to a property of a property? Say the datasource="Cars" which is a collection of car classes and the...
0
897
by: freddie | last post by:
Hi, I am adding the data binding feature to the list view control. I am bale to populate the column collection dynamically from the datasource. I would like to allow the user to remove or add any...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.