>>using System; >>using System.Collections.Generic; >>using System.Linq; >>using System.Text; >>using System.IO; >>using System.Data; >>using System.Data.Sql; >>using System.Data.SqlClient; >> >>namespace ConsoleApplication1 >>{ >> class MySample >> { >> public delegate void MyRowHandler(DataRow r); >> public event MyRowHandler MyEvent; // You need to declare the event. Its type is a delegate // A delegate is a 'pointer' to a method // Check my blog "Some more C# 3.0 features" there have more info on delegates and sample // Here we are saying that any method that have the same signature as MyRowHandler // gets a DataRow and returns void // can be 'added' to our invocation when the event is raised // you can add or remove 'action' delegates using + and - operators >> >> static void Main(string[] args) >> { >> MySample s = new MySample(); // Instantiated our class >> s.MyEvent += new MyRowHandler(MyHandler1); >> s.MyEvent += new MyRowHandler(MyHandler2); >> s.MyEvent += oRow => >> Console.WriteLine("Lambda expression call to my event handler: {0}", >> oRow.Field<string>("CompanyName")); // Added 3 delegates to s - an instance of MySample // MyHandler1 and MyHandler2 are registered as delegated method calls // they have the same sinature as MyRowHandler // this is a more verbose way to add first one: // MyRowHandler h1 = MyHandler1; // we are saying h1 is of type MyRowHandler and is a pointer to h1 // s.MyEvent += h1; // attach to invocation list // 3rd delegate added is only using a different syntax (lambda expresion) - again check blog >> >> DataSet MyData = new DataSet(); >> SqlConnection cn = new SqlConnection(@"server=.\sqlexpress;trusted_connection=yes;database=Northwind"); >> SqlCommand cmd = new SqlCommand("select * from customers where country = 'USA'", cn); >> cn.Open(); >> SqlDataReader rdr = cmd.ExecuteReader(); >> MyData.Tables.Add(); >> MyData.Tables[0].Load(rdr); >> cn.Close(); >> >> foreach (DataRow oRow in MyData.Tables[0].Rows) >> { // since events may or may not have any invocation items check if it is null or not >> if (s.MyEvent != null) // raise the event if it has members // note that actually "s" instance and the dataset are unrelated >> s.MyEvent(oRow); >> } >> >> >> } >> >> private static void MyHandler1(DataRow row) >> { >> Console.WriteLine("MyHandler1 called: {0}", row[0].ToString()); >> } >> private static void MyHandler2(DataRow row) >> { >> Console.WriteLine("MyHandler2 called: {0}", row[0].ToString()); >> } >> } >>}Cetin