I reproduced your code (with minor reformatting) and it executes correctly.
Note that "txtmesssage" is an incorrect spelling of "txtmessage". So the question is, "Did this code actually compile?"
I think that you have some serious problems with naming variables. Also the variable declarations of the form:
private int _numero;
public int Numero
{
get { return _numero; }
set { _numero = value; }
}
should, IMO, be replaced by something like
public int Numero { get; set; }
You get a significant decrease in LOC.
The bool test of the form
if ( recherche ( ctn.Numero ) == true )
should be replaced by
if ( recherche ( ctn.Numero ) )
The following is your code rewritten that works. There were no substantive coding changes:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace TestingListe
{
public partial class Form1 : Form
{
public Form1 ( )
{
InitializeComponent ( );
}
private void btnajouter_Click ( object sender,
EventArgs e )
{
Contact contact = new Contact ( );
contact.Numero = int.Parse ( txtnumero.Text );
contact.Nom = txtNom.Text;
contact.Prenom = txtPrenom.Text;
contact.Message = txtmessage.Text;
Contacts.Ajouter ( contact );
dgvcontacts.DataSource = null;
dgvcontacts.DataSource = Contacts.ct;
}
}
static class Program
{
[STAThread]
static void Main ( )
{
Application.EnableVisualStyles ( );
Application.SetCompatibleTextRenderingDefault ( false );
Application.Run ( new Form1 ( ) );
}
}
public class Contact
{
public int Numero { get; set; }
public string Nom { get; set; }
public string Prenom { get; set; }
public string Message { get; set; }
public Contact ( )
{
}
public Contact ( int numero,
string nom,
string prenom,
string message )
{
this.Numero = numero;
this.Nom = nom;
this.Prenom = prenom;
this.Message = message;
}
}
public static class Contacts
{
public static List<contact> ct = new List<contact> ( );
public static bool recherche ( int num )
{
foreach ( Contact search in ct )
{
if ( search.Numero == num )
{
return false;
}
}
return true;
}
public static bool Ajouter ( Contact ctn )
{
if ( recherche ( ctn.Numero ) == true )
{
ct.Add(ctn);
return true;
}
return false;
}
}
}
</contact></contact>
Hope that helps.