Page d'Accueil

Gestion des Enregistrement : Supprimer les Enregistrements

 

Supprimer la rangée courante

S'il vous arrive justement d'avoir un enregistrement que vous n'avez pas besoin ou ne trouvez pas nécessaire dans un tableau, vous pouvez enlever cet enregistrement du tableau. Pour commencer, vous devez établir quel enregistrement vous voulez supprimer. De nouveau, vous auriez besoin d'identifier d'une manière unique un enregistrement. Pour notre collection de vidéos, vous pouvez utiliser la colonne du nombre d'étagère. Une fois que vous avez localisé l'enregistrement peu désiré, vous pouvez le supprimer.

Pour vous aider à enlever un enregistrement, la classe DataRow est équipée d'une méthode appelée Delete. Sa syntaxe est simplement :

public void Delete();

Cette méthode doit s'appeler par l'enregistrement qui a besoin d'être supprimé. Voici un exemple:

using System;
using System.IO;
using System.Xml;
using System.Data;
using System.Collections;

namespace VideoCollection3
{
    public class VideoCollection
    {

        . . . No Change

        public void DeleteVideo()
        {
            if (File.Exists(strFilename))
            {
                bool found = false;                
                string strShelfNumber = "AA-000";

                dsVideos.ReadXml(strFilename);

                Console.WriteLine("\nHere is the current list of videos");

                Console.WriteLine("========================================");
                Console.WriteLine("      Video Collection");
                Console.WriteLine("=========================================");
                foreach (DataRow vdo in tblVideos.Rows)
                {
                    Console.WriteLine("Shelf #:  {0}", vdo["ShelfNumber"]);
                    Console.WriteLine("Title:    {0}", vdo["Title"]);
                    Console.WriteLine("Director: {0}", vdo["Director"]);
                    Console.WriteLine("(c) Year: {0}", vdo["Year"]);
                    Console.WriteLine("Length:   {0:C}", vdo["Length"]);
                    Console.WriteLine("Rating:   {0}", vdo["Rating"]);
                    Console.WriteLine("-----------------------------------------");
                }

                Console.Write(
		    "Enter the shelf number of the video you want to delete: ");
                strShelfNumber = Console.ReadLine();

                foreach (DataRow vdo in tblVideos.Rows)
                {
                    string str = (string)vdo["ShelfNumber"];

                    if (str == strShelfNumber)
                    {
                        found = true;

                        Console.WriteLine(
				"\n-----------------------------------------");
                        Console.WriteLine("Here is the video you want to delete");
                        Console.WriteLine("1. Title:    {0}", vdo["Title"]);
                        Console.WriteLine("2. Director: {0}", vdo["Director"]);
                        Console.WriteLine("3. (c) Year: {0}", vdo["Year"]);
                        Console.WriteLine("4. Length:   {0}", vdo["Length"]);
                        Console.WriteLine("5. Rating:   {0}", vdo["Rating"]);
                        Console.WriteLine(
				"-----------------------------------------");

                        Console.Write(
				"Do you still want to delete this video (y/n)? ");
                        char ans = char.Parse(Console.ReadLine());

                        if ((ans == 'y') || (ans == 'Y'))
                        {
                            vdo.Delete();
                            dsVideos.WriteXml(strFilename);
                            Console.WriteLine("The video has been deleted!");
                        }
                        
                        return;
                    }
                }

                if (found == false)
                {
                    Console.WriteLine("No video with that shelf number was found");
                    return;
                }
            }
        }
    }
}

Enlever une rangée de la collection des enregistrements

En plus de la classe DataRow, la classe DataRowCollection fournit ses propres moyens de supprimer un enregistrement d'un tableau. Pour supprimer un enregistrement, vous pouvez appeler la méthode DataRowCollection.Remove (). Sa syntaxe est :

public void Remove(DataRow row);

Cette méthode prend comme argument un objet DataRow et vérifie si le tableau le contient. Si cet enregistrement existe, il est supprimé, y compris toutes ses entrées pour chaque colonne. Voici un exemple:

using System;
using System.IO;
using System.Xml;
using System.Data;
using System.Collections;

namespace VideoCollection3
{
    public class VideoCollection
    {

        . . . No Change

        public void DeleteVideo()
        {
            if (File.Exists(strFilename))
            {
                bool found = false;                
                string strShelfNumber = "AA-000";

                dsVideos.ReadXml(strFilename);

                Console.WriteLine("\nHere is the current list of videos");

                Console.WriteLine("========================================");
                Console.WriteLine("      Video Collection");
                Console.WriteLine("=========================================");
                foreach (DataRow vdo in tblVideos.Rows)
                {
                    Console.WriteLine("Shelf #:  {0}", vdo["ShelfNumber"]);
                    Console.WriteLine("Title:    {0}", vdo["Title"]);
                    Console.WriteLine("Director: {0}", vdo["Director"]);
                    Console.WriteLine("(c) Year: {0}", vdo["Year"]);
                    Console.WriteLine("Length:   {0:C}", vdo["Length"]);
                    Console.WriteLine("Rating:   {0}", vdo["Rating"]);
                    Console.WriteLine("-----------------------------------------");
                }

                Console.Write(
		    "Enter the shelf number of the video you want to delete: ");
                strShelfNumber = Console.ReadLine();

                foreach (DataRow vdo in tblVideos.Rows)
                {
                    string str = (string)vdo["ShelfNumber"];

                    if (str == strShelfNumber)
                    {
                        found = true;

                        Console.WriteLine(
				"\n-----------------------------------------");
                        Console.WriteLine("Here is the video you want to delete");
                        Console.WriteLine("1. Title:    {0}", vdo["Title"]);
                        Console.WriteLine("2. Director: {0}", vdo["Director"]);
                        Console.WriteLine("3. (c) Year: {0}", vdo["Year"]);
                        Console.WriteLine("4. Length:   {0}", vdo["Length"]);
                        Console.WriteLine("5. Rating:   {0}", vdo["Rating"]);
                        Console.WriteLine(
				"-----------------------------------------");

                        Console.Write(
				"Do you still want to delete this video (y/n)? ");
                        char ans = char.Parse(Console.ReadLine());

                        if ((ans == 'y') || (ans == 'Y'))
                        {
                            tblVideos.Rows.Remove(vdo);
                            dsVideos.WriteXml(strFilename);
                            Console.WriteLine("The video has been deleted!");
                        }
                        
                        return;
                    }
                }

                if (found == false)
                {
                    Console.WriteLine("No video with that shelf number was found");
                    return;
                }
            }
        }
    }
}

Supprimer un enregistrement par son index

En appelant la méthode DataRowCollection.Remove (), vous devez passer une identification exacte de l'enregistrement. Si vous n'avez pas cette identification, vous pouvez supprimer un enregistrement basé sur son index. Pour ce faire, vous appelleriez la méthode DataRowCollection.RemoveAt (). Sa syntaxe est :

public void RemoveAt(int index);

Cette méthode prend comme argument l'index de l'enregistrement que vous voulez supprimer. Si un enregistrement avec cet index existe, il serait supprimé.

Supprimer tous les enregistrements d'un Tableau

Pour supprimer tous les enregistrements d'un tableau, appelez la méthode DataRowCollection.Clear (). Sa syntaxe est :

public void Clear();

Cette méthode est utilisée pour dégager le tableau de tous les enregistrements.

  

Précédent Copyright © 2007, Yevol Suivant