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/is...g/default.aspx.
The problem is the article is in VisualBasic. I already get the collection
to be recognized as a Data Source by the IDE. It populated the DataGrid
correctly from the fields on the items object of the collection, but I can't
get the DataGrid to show the collection content at run time. It is my first
ever data binded form so may be the error is not with the collection itself
but with the handling of the DataGrid and Form itself.
My collection (DPersonaLightC ollection) is a type safe collection of
DPersonaLight objects. It inherits from CollectionBase, and implements
IComponent, IBindingList for design time binding support. It looks to be
work good. The code for the WindowsForm that shows the collection is the
following. The DataGrid is named dataGridLocator . At design time I added the
control fPersonaLightCo llection. The dataGridLocator was set up at design
time with DataSource fPersonaLightCo llection. I left DataMember in blank. On
the Form Load event I fill fPersonaLightCo llection with 3 objects to show.
When the program runs, nothing is displayed, only the column headers that
were loaded at design time. What I'm doing wrong?
Thanks in advance
Sammy
using System;
using System.Data;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using DBS.Interfaces. Basics;
using DBS.Interfaces. Persona;
namespace DBS.Client.Test
{
/// <summary>
/// Summary description for FormPersona.
/// </summary>
public class FormPersona : System.Windows. Forms.Form
{
private System.Windows. Forms.TabContro l tabControl;
private System.Windows. Forms.TabPage tabLocator;
private System.Windows. Forms.TabPage tabGeneral;
private System.Windows. Forms.TabPage tabPage3;
private System.Windows. Forms.DataGrid dataGridLocator ;
private System.Windows. Forms.TextBox textBoxDescript ion;
private System.Windows. Forms.Label label1;
private System.Windows. Forms.TextBox textBoxSearchNo mbre;
private System.Windows. Forms.Button buttonSearch;
private DBS.Interfaces. Persona.DPerson aLightCollectio n
fPersonaLightCo llection;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;
public FormPersona()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
//
// TODO: Add any constructor code after InitializeCompo nent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.tabControl = new System.Windows. Forms.TabContro l();
this.tabLocator = new System.Windows. Forms.TabPage() ;
this.buttonSear ch = new System.Windows. Forms.Button();
this.textBoxSea rchNombre = new System.Windows. Forms.TextBox() ;
this.label1 = new System.Windows. Forms.Label();
this.dataGridLo cator = new System.Windows. Forms.DataGrid( );
this.tabGeneral = new System.Windows. Forms.TabPage() ;
this.tabPage3 = new System.Windows. Forms.TabPage() ;
this.textBoxDes cription = new System.Windows. Forms.TextBox() ;
this.fPersonaLi ghtCollection = new
DBS.Interfaces. Persona.DPerson aLightCollectio n();
this.tabControl .SuspendLayout( );
this.tabLocator .SuspendLayout( );
((System.Compon entModel.ISuppo rtInitialize)(t his.dataGridLoc ator)).BeginIni t();
this.SuspendLay out();
//
// tabControl
//
this.tabControl .Anchor = (((System.Windo ws.Forms.Anchor Styles.Top |
System.Windows. Forms.AnchorSty les.Bottom)
| System.Windows. Forms.AnchorSty les.Left)
| System.Windows. Forms.AnchorSty les.Right);
this.tabControl .Controls.AddRa nge(new System.Windows. Forms.Control[] {
this.tabLocator ,
this.tabGeneral ,
this.tabPage3}) ;
this.tabControl .Location = new System.Drawing. Point(8, 80);
this.tabControl .Name = "tabControl ";
this.tabControl .SelectedIndex = 0;
this.tabControl .Size = new System.Drawing. Size(712, 400);
this.tabControl .TabIndex = 0;
//
// tabLocator
//
this.tabLocator .Controls.AddRa nge(new System.Windows. Forms.Control[] {
this.buttonSear ch,
this.textBoxSea rchNombre,
this.label1,
this.dataGridLo cator});
this.tabLocator .Location = new System.Drawing. Point(4, 22);
this.tabLocator .Name = "tabLocator ";
this.tabLocator .Size = new System.Drawing. Size(704, 374);
this.tabLocator .TabIndex = 0;
this.tabLocator .Text = "Localizado r";
//
// buttonSearch
//
this.buttonSear ch.Location = new System.Drawing. Point(216, 8);
this.buttonSear ch.Name = "buttonSear ch";
this.buttonSear ch.Size = new System.Drawing. Size(88, 24);
this.buttonSear ch.TabIndex = 3;
this.buttonSear ch.Text = "Buscar";
this.buttonSear ch.Click += new
System.EventHan dler(this.butto nSearch_Click);
//
// textBoxSearchNo mbre
//
this.textBoxSea rchNombre.Locat ion = new System.Drawing. Point(56, 8);
this.textBoxSea rchNombre.Name = "textBoxSearchN ombre";
this.textBoxSea rchNombre.Size = new System.Drawing. Size(152, 20);
this.textBoxSea rchNombre.TabIn dex = 2;
this.textBoxSea rchNombre.Text = "";
//
// label1
//
this.label1.Loc ation = new System.Drawing. Point(8, 8);
this.label1.Nam e = "label1";
this.label1.Siz e = new System.Drawing. Size(48, 16);
this.label1.Tab Index = 1;
this.label1.Tex t = "Nombre:";
//
// dataGridLocator
//
this.dataGridLo cator.Anchor = (((System.Windo ws.Forms.Anchor Styles.Top |
System.Windows. Forms.AnchorSty les.Bottom)
| System.Windows. Forms.AnchorSty les.Left)
| System.Windows. Forms.AnchorSty les.Right);
this.dataGridLo cator.DataMembe r = "";
this.dataGridLo cator.DataSourc e = this.fPersonaLi ghtCollection;
this.dataGridLo cator.HeaderFor eColor =
System.Drawing. SystemColors.Co ntrolText;
this.dataGridLo cator.Location = new System.Drawing. Point(8, 32);
this.dataGridLo cator.Name = "dataGridLocato r";
this.dataGridLo cator.Size = new System.Drawing. Size(688, 336);
this.dataGridLo cator.TabIndex = 0;
//
// tabGeneral
//
this.tabGeneral .Location = new System.Drawing. Point(4, 22);
this.tabGeneral .Name = "tabGeneral ";
this.tabGeneral .Size = new System.Drawing. Size(704, 374);
this.tabGeneral .TabIndex = 1;
this.tabGeneral .Text = "Generales" ;
//
// tabPage3
//
this.tabPage3.L ocation = new System.Drawing. Point(4, 22);
this.tabPage3.N ame = "tabPage3";
this.tabPage3.S ize = new System.Drawing. Size(704, 374);
this.tabPage3.T abIndex = 2;
this.tabPage3.T ext = "tabPage3";
//
// textBoxDescript ion
//
this.textBoxDes cription.Anchor = ((System.Window s.Forms.AnchorS tyles.Top
| System.Windows. Forms.AnchorSty les.Left)
| System.Windows. Forms.AnchorSty les.Right);
this.textBoxDes cription.Enable d = false;
this.textBoxDes cription.Locati on = new System.Drawing. Point(8, 8);
this.textBoxDes cription.Multil ine = true;
this.textBoxDes cription.Name = "textBoxDescrip tion";
this.textBoxDes cription.Size = new System.Drawing. Size(712, 64);
this.textBoxDes cription.TabInd ex = 1;
this.textBoxDes cription.Text = "";
//
// FormPersona
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(728, 486);
this.Controls.A ddRange(new System.Windows. Forms.Control[] {
this.textBoxDes cription,
this.tabControl });
this.Name = "FormPerson a";
this.Text = "FormPerson a";
this.Load += new System.EventHan dler(this.FormP ersona_Load);
this.tabControl .ResumeLayout(f alse);
this.tabLocator .ResumeLayout(f alse);
((System.Compon entModel.ISuppo rtInitialize)(t his.dataGridLoc ator)).EndInit( );
this.ResumeLayo ut(false);
}
#endregion
private void buttonSearch_Cl ick(object sender, System.EventArg s e)
{
UIControllerPer sona ctrlPersona = new UIControllerPer sona();
ctrlPersona.Ini tializeRemoting ();
DDataCollection Container containerPerson as = ctrlPersona.Sea rchPersona();
this.fPersonaLi ghtCollection =
(DPersonaLightC ollection)conta inerPersonas.Co llection;
this.dataGridLo cator.Refresh() ;
}
private void FormPersona_Loa d(object sender, System.EventArg s e)
{
DPersonaLight persona = new DPersonaLight() ;
persona.Apellid oMaterno.Value = "KK1";
persona.Apellid oPaterno.Value = "KK2";
persona.Direcci onPostal.Value = "KK3";
persona.Id.Valu e = 1;
persona.Key.Val ue = "KK4";
persona.Nombres .Value = "KK5";
persona.Telefon os.Value = "KK6";
this.fPersonaLi ghtCollection.A dd(persona);
persona = new DPersonaLight() ;
persona.Apellid oMaterno.Value = "PP1";
persona.Apellid oPaterno.Value = "PP2";
persona.Direcci onPostal.Value = "PP3";
persona.Id.Valu e = 2;
persona.Key.Val ue = "PP4";
persona.Nombres .Value = "PP5";
persona.Telefon os.Value = "PP6";
this.fPersonaLi ghtCollection.A dd(persona);
}
}
}
The DPersonaLightCo llection class is:
using System;
using System.Collecti ons;
using System.Componen tModel;
namespace DBS.Interfaces. Persona
{
/// <summary>
/// Summary description for DPersonaLightCo llection.
/// </summary>
[Serializable]
public class DPersonaLightCo llection : CollectionBase, IComponent,
IBindingList
{
private ISite fSite;
public DPersonaLightCo llection() : base()
{
fSite = null;
}
//--- IBindingList ---
public event ListChangedEven tHandler ListChanged;
public bool AllowEdit { get { return false; } }
public bool AllowNew { get { return false; } }
public bool AllowRemove { get { return false; } }
public bool IsSorted { get { return false; } }
public ListSortDirecti on SortDirection { get { throw new
NotSupportedExc eption(); } }
public PropertyDescrip tor SortProperty {get { throw new
NotSupportedExc eption(); }}
public bool SupportsChangeN otification {get { return false;} }
public bool SupportsSearchi ng {get { return false;} }
public bool SupportsSorting {get { return false;} }
public void AddIndex(Proper tyDescriptor property) { }
public object AddNew() { throw new NotSupportedExc eption(); }
public void ApplySort(Prope rtyDescriptor property,ListSo rtDirection
direction) { throw new NotSupportedExc eption(); }
public int Find(PropertyDe scriptor property,object key) { throw new
NotSupportedExc eption(); }
public void RemoveIndex(Pro pertyDescriptor property) {}
public void RemoveSort(){ throw new NotSupportedExc eption(); }
//--- IComponent ---
public event EventHandler Disposed;
public ISite Site
{
get { return fSite; }
set { fSite = value; }
}
public void Dispose()
{
this.List.Clear ();
if (this.fSite != null && this.fSite.Cont ainer != null)
fSite.Container .Remove(this);
Disposed(this, System.EventArg s.Empty);
}
//--- IList ---
public int Add(DPersonaLig ht aPersonaLight)
{
return this.List.Add(a PersonaLight);
}
public void Remove(DPersona Light aPersonaLight)
{
this.List.Remov e(aPersonaLight );
}
public bool Contains(DPerso naLight aPersonaLight)
{
return this.List.Conta ins(aPersonaLig ht);
}
public int IndexOf(DPerson aLight aPersonaLight)
{
return this.List.Index Of(aPersonaLigh t);
}
public void Insert(int index, DPersonaLight aPersonaLight)
{
this.List.Inser t(index, aPersonaLight);
}
public void CopyTo(Array array, int start)
{
List.CopyTo(arr ay, start);
}
public DPersonaLight this[int index]
{
get { return (DPersonaLight) List[index]; }
set { Insert(index,va lue); }
}
protected override void OnValidate(obje ct value)
{
base.OnValidate (value);
if (!(value is DPersonaLight))
{
throw new ArgumentExcepti on("Collection only supports DPersonaLight
objects.");
}
}
}
}