在datagridview中显示 [英] Displaying in datagridview
问题描述
我的问题是关于datagridview。我用这个源代码创建了一个类ctsContacts:
my question is about the datagridview. I create a class ctsContacts with this source code:
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
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
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;
问题是在datagridview显示右侧复制标题。
显示
我的尝试:
i尝试使用此代码:
the problem is at the datagridview display that copy header right.
display
What I have tried:
i try to the code with this:
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;
}
}
}
和按钮ajouter与此:
and the button ajouter with this:
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
推荐答案
我复制了你的代码(带有轻微的重新格式化)并且它正确执行。
请注意,txtmesssage是txtmessage的拼写错误。所以问题是,这段代码实际编译了吗?
我认为你在命名变量方面存在一些严重问题。表格的变量声明:
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; }
}
应该由IMO代替
public int Numero {get;组; }
你的LOC大幅下降。
表格的布尔测试
if(recherche(ctn.Numero)== true)
应替换为
if(recherche(ctn.Numero))
以下是您重写的代码有效。没有实质性的编码更改:
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
{
// *************************************************** 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.
这篇关于在datagridview中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!