dataGridView userDeletingRows

  • Thread starter Thread starter noom
  • Start date Start date
N

noom

Hello
I`m trying to use userDeletingRows event but can`t deem to make it
work.
I tried :

this.dataGridView1.UserDeletedRow += new
DataGridViewRowCancelEventArgs

- but I get an error message on the arguments/
do I need to use a delegate (if so - which is it)?

can someone please post a full working example? (msdn didn`t help at
all...)

thnx
 
Yes you can only subscribe an delegate to an event.

UserDeletedRow is an event that accepts a DataGridViewRowEventHandler
UserDeletingRow is an event that accepts a
DataGridViewRowCancelEventHandler

Here's an example (using C# 3 syntax) that only allows delete on odd
rows; if the row index is even the delete is cancelled:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.ComponentModel;

class Foo
{
public int Bar { get; set; }
public int Bloop { get; set; }
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
BindingList<Foo> foos = new BindingList<Foo>();
for (int i = 0; i < 100; i++)
{
foos.Add(new Foo { Bar = i, Bloop = 200 - i });
}
DataGridView dgv = new DataGridView
{
Dock = DockStyle.Fill,
DataSource = foos
};
dgv.UserDeletingRow += (sender, args) =>
{
args.Cancel = args.Row.Index % 2 == 0;
};
Application.Run(new Form
{
Controls = { dgv }
});

}
}
 
Back
Top