Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Define Custom Event
Message
De
16/02/2009 18:39:56
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01382215
Message ID:
01382227
Vues:
31
>One of the two areas in C# I'm still foggy in is Events. I have a need for an event, and a practicle example is a good learning tool.
>
>I am interating through a dataset, and I want to raise an event for each row. I'm unsure as to what goes here:
>
>
>foreach (DataRow oRow in MyData.Tables[0].Rows)
>{
>
>}
>
>
>
>Can someone help a newbie?

Here is a sample (added dataset just because you had one):
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;

        static void Main(string[] args)
        {
            MySample s = new MySample();
            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"));

            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)
            {
                if (s.MyEvent != null)
                    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
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform