DataGridView Events

K

Kali

What are the events handlers I need to insert / update / delete rows in a
datagridview? Seems a lot different than asp.net. I can initially bind
the datagridview but don't know the event handlers to add/update/delete.
I'd appreciate any insight. Thanks a lot.
 
V

vanderghast

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top