If you create your own made data source, one way is to implement
IBindingList and to define an empty constructor (to add new "row").
BindingList><T> does implement IBindingList. You can use BindingSource to
expose IBindingList on types that do not implement IBindingList. Here an
example doing it for 'data' supported by class "Toto" :
- Create a windows form project, VerySimpleDataGridView
- In form1, add a datagridview, dataGridView1
- In Form1.cs, have the code:
=====================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace VerySimpleDataGridView
{
public partial class Form1 : Form
{
BindingSource mydataSourceBS = new BindingSource();
public Form1()
{
InitializeComponent();
mydataSourceBS.DataSource = typeof(Toto);
this.dataGridView1.DataSource = mydataSourceBS;
mydataSourceBS.Add(new Toto("Yoman", "World", 2010));
}
}
public class Toto
{
public String firstName { get; set; }
public String lastName { get; set; }
public decimal someThing { get; set; }
public Toto() { }
public Toto(String fName, String lName, decimal what)
{
firstName = fName;
lastName = lName;
someThing = what;
}
}
======================
- Run.
- Note that you can add new rows.
- Comment the empty constructor for Toto.
- Run
- You can't add row anymore.
More useful details in "Windows Forms 2.0 Programming", by Sells and
Weinhardt, at Addison Wesley, ch 16 and 17.
Vanderghast, Access MVP