Click here to Skip to main content
15,513,489 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi, my question is about the datagridview. I create a class ctsContacts with this source code:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace TestingListe
{
    class ctsContacts:lstContacts
    {
        private int _numero;
 
        public int Numero
        {
            get { return _numero; }
            set { _numero = value; }
        }
 
        private string _nom;
 
        public string Nom
        {
            get { return _nom; }
            set { _nom = value; }
        }
        private string _prenom;
 
        public string Prenom
        {
            get { return _prenom; }
            set { _prenom = value; }
        }
        private string _message;
 
        public string Message
        {
            get { return _message; }
            set { _message = value; }
        }
 
 
        public ctsContacts()
        {
 
        }
 
        public ctsContacts(int numero, string nom,string prenom,string message)
        {
            this.Numero = numero;
            this.Nom = nom;
            this.Prenom = prenom;
            this.Message = message;
        }
 
        public bool recherche(int num)
        {
            foreach (ctsContacts search in ct)
            {
                if (search.Numero == num)
                {
                    return false;
                }
 
            }
            return true;
 
        }
 
        public bool Ajouter(ctsContacts ctn)
        {
            if (this.recherche(ctn.Numero) == true)
            {
                ct.Add(ctn);
                return true;
            }
            return false;
        }
 
    }



This class inherits another abstract class that contains a list
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace TestingListe
{
    class lstContacts
    {
        public static List<ctsContacts> ct = new List<ctsContacts>();
        public lstContacts()
        {
 
        }
 
    }



and then in the contact form and add button at this writing source code
C#
private void btnajouter_Click(object sender, EventArgs e)
        {
            ctsContacts contact = new ctsContacts();
            contact.Numero = int.Parse(txtnumero.Text);
            contact.Nom = txtNom.Text;
            contact.Prenom = txtPrenom.Text;
            contact.Message = txtmesssage.Text;
 
            contact.Ajouter(contact);
 
            dgvcontacts.DataSource = null;
 
            dgvcontacts.DataSource = ctsContacts.ct;



the problem is at the datagridview display that copy header right.

display

What I have tried:

i try to the code with this:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace TestingListe
{
    public class ctsContact
    {
        private int _numero;
 
        public int Numero
        {
            get { return _numero; }
            set { _numero = value; }
        }
 
        private string _nom;
 
        public string Nom
        {
            get { return _nom; }
            set { _nom = value; }
        }
        private string _prenom;
 
        public string Prenom
        {
            get { return _prenom; }
            set { _prenom = value; }
        }
        private string _message;
 
        public string Message
        {
            get { return _message; }
            set { _message = value; }
        }
 
 
        public ctsContact()
        {
 
        }
 
        public ctsContact(int numero, string nom,string prenom,string message)
        {
            this.Numero = numero;
            this.Nom = nom;
            this.Prenom = prenom;
            this.Message = message;
        }
 
    }
    public static class ctsContacts
    {
        public static List<ctsContact> ct = new List<ctsContact>();
        public static bool recherche(int num)
        {
            foreach (ctsContact search in ct)
            {
                if (search.Numero == num)
                {
                    return false;
                }
 
            }
            return true;
 
        }
 
        public static bool Ajouter(ctsContact ctn)
        {
            if (recherche(ctn.Numero) == true)
            {
                ct.Add(ctn);
                return true;
            }
            return false;
        }
    }
}


and the button ajouter with this:

C#
private void btnajouter_Click(object sender, EventArgs e)
        {
            ctsContact contact = new ctsContact();
            contact.Numero = int.Parse(txtnumero.Text);
            contact.Nom = txtNom.Text;
            contact.Prenom = txtPrenom.Text;
            contact.Message = txtmesssage.Text;
 
            ctsContacts.Ajouter(contact);
 
            dgvcontacts.DataSource = null;
 
            dgvcontacts.DataSource = ctsContacts.ct;


but i have the same problem
Posted
Updated 18-Oct-16 8:32am
Comments
[no name] 17-Oct-16 14:39pm    
"my question", there is no question anywhere in your posting. What question is it?
"same problem", no problem described anywhere in your posting either, so what problem?
Swinkaran 17-Oct-16 17:40pm    
Looks like few things are wrong. Not clear what you are trying to.
Did you try
dgvcontacts.Databind();
Karthik_Mahalingam 18-Oct-16 4:14am    
its windows form application
Databind(); is applicable to web app

1 solution

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:

C#
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:

C#
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace TestingListe
    {

    // *************************************************** class Form1
    
    public partial class Form1 : Form
        {
        
        // ***************************************************** Form1
        
        public Form1 ( )
            {
            InitializeComponent ( );
            }

        // ****************************************** btnajouter_Click
        
        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 = txtmesssage.Text; // txtmesssage???
            contact.Message = txtmessage.Text; // 

            Contacts.Ajouter ( contact );

            dgvcontacts.DataSource = null;

            dgvcontacts.DataSource = Contacts.ct;
            }

        }  // class Form1

    // ************************************************* class Program
    
    static class Program
        {

        [STAThread]
        static void Main ( )
            {
            Application.EnableVisualStyles ( );
            Application.SetCompatibleTextRenderingDefault ( false );
            Application.Run ( new Form1 ( ) );
            }

        } // class Program

    // ************************************************* class Contact
    
    public class Contact
        {
        public int Numero { get; set; }
        public string Nom { get; set; }
        public string Prenom { get; set; }
        public string Message { get; set; }

        // *************************************************** Contact
        
        public Contact ( )
            {
 
            }
 
        // *************************************************** Contact
        
        public Contact ( int    numero, 
                         string nom,
                         string prenom,
                         string message )
            {
            
            this.Numero = numero;
            this.Nom = nom;
            this.Prenom = prenom;
            this.Message = message;
            }
 
        }
        
    // ************************************************ class Contacts
    
    public static class Contacts
        {
        public static List<contact> ct = new List<contact> ( );
        
        // ************************************************* recherche
        
        public static bool recherche ( int num )
            {
            
            foreach ( Contact search in ct )
                {
                if ( search.Numero == num )
                    {
                    return false;
                    }
 
                }
            return true;
 
            }
 
        // *************************************************** Ajouter
        
        public static bool Ajouter ( Contact ctn )
            {
            if ( recherche ( ctn.Numero ) == true )
                {
                ct.Add(ctn);
                return true;
                }
            return false;
            }
        
        } // class Contacts
        
    } // namespace TestingListe

</contact></contact>


Hope that helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900