1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
using System.ComponentModel;
namespace TestDataGridView
{
internal static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new MyForm());
}
}
public class MyForm : Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
nameTextBox = new TextBox();
firstNameTextBox = new TextBox();
nameLabel = new Label();
firstNameLabel = new Label();
personListDataGridView = new DataGridView();
nameDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
firstNameDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
personBindingSource = new BindingSource(components);
newPersonButton = new Button();
((System.ComponentModel.ISupportInitialize)personListDataGridView).BeginInit();
((System.ComponentModel.ISupportInitialize)personBindingSource).BeginInit();
SuspendLayout();
//
// nameTextBox
//
nameTextBox.Location = new Point(78, 24);
nameTextBox.Name = "nameTextBox";
nameTextBox.Size = new Size(174, 23);
nameTextBox.TabIndex = 0;
//
// firstNameTextBox
//
firstNameTextBox.Location = new Point(78, 53);
firstNameTextBox.Name = "firstNameTextBox";
firstNameTextBox.Size = new Size(174, 23);
firstNameTextBox.TabIndex = 1;
//
// nameLabel
//
nameLabel.AutoSize = true;
nameLabel.Location = new Point(12, 27);
nameLabel.Name = "nameLabel";
nameLabel.Size = new Size(37, 15);
nameLabel.TabIndex = 2;
nameLabel.Text = "name";
//
// firstNameLabel
//
firstNameLabel.AutoSize = true;
firstNameLabel.Location = new Point(12, 56);
firstNameLabel.Name = "firstNameLabel";
firstNameLabel.Size = new Size(60, 15);
firstNameLabel.TabIndex = 3;
firstNameLabel.Text = "first name";
//
// personListDataGridView
//
personListDataGridView.AllowUserToResizeColumns = false;
personListDataGridView.AllowUserToResizeRows = false;
personListDataGridView.AutoGenerateColumns = false;
personListDataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
personListDataGridView.Columns.AddRange(new DataGridViewColumn[] { nameDataGridViewTextBoxColumn, firstNameDataGridViewTextBoxColumn });
personListDataGridView.DataSource = personBindingSource;
personListDataGridView.Location = new Point(12, 82);
personListDataGridView.Name = "personListDataGridView";
personListDataGridView.RowTemplate.Height = 25;
personListDataGridView.Size = new Size(409, 284);
personListDataGridView.TabIndex = 4;
//
// nameDataGridViewTextBoxColumn
//
nameDataGridViewTextBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
nameDataGridViewTextBoxColumn.DataPropertyName = "Name";
nameDataGridViewTextBoxColumn.HeaderText = "Name";
nameDataGridViewTextBoxColumn.Name = "nameDataGridViewTextBoxColumn";
//
// firstNameDataGridViewTextBoxColumn
//
firstNameDataGridViewTextBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
firstNameDataGridViewTextBoxColumn.DataPropertyName = "FirstName";
firstNameDataGridViewTextBoxColumn.HeaderText = "FirstName";
firstNameDataGridViewTextBoxColumn.Name = "firstNameDataGridViewTextBoxColumn";
//
// personBindingSource
//
personBindingSource.DataSource = typeof(Person);
//
// newPersonButton
//
newPersonButton.Location = new Point(12, 372);
newPersonButton.Name = "newPersonButton";
newPersonButton.Size = new Size(121, 23);
newPersonButton.TabIndex = 5;
newPersonButton.Text = "new person";
newPersonButton.UseVisualStyleBackColor = true;
newPersonButton.Click += newPersonButton_Click;
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
AutoSize = true;
ClientSize = new Size(433, 407);
Controls.Add(newPersonButton);
Controls.Add(personListDataGridView);
Controls.Add(firstNameLabel);
Controls.Add(nameLabel);
Controls.Add(firstNameTextBox);
Controls.Add(nameTextBox);
Name = "Form1";
Text = "Form1";
((System.ComponentModel.ISupportInitialize)personListDataGridView).EndInit();
((System.ComponentModel.ISupportInitialize)personBindingSource).EndInit();
ResumeLayout(false);
PerformLayout();
}
#endregion
private TextBox nameTextBox;
private TextBox firstNameTextBox;
private Label nameLabel;
private Label firstNameLabel;
private DataGridView personListDataGridView;
private Button newPersonButton;
private BindingSource personBindingSource;
private DataGridViewTextBoxColumn nameDataGridViewTextBoxColumn;
private DataGridViewTextBoxColumn firstNameDataGridViewTextBoxColumn;
private static int _counter = 0;
private IList<Person> _persons;
public MyForm()
{
InitializeComponent();
// Remplacer ici la création de la liste par une List<Person>, et on perd le two-way binding
//_persons = new List<Person>(this.GetPersons());
_persons = new BindingList<Person>(this.GetPersons());
this.personBindingSource.DataSource = _persons;
this.firstNameTextBox.DataBindings.Add(new Binding("Text", this.personBindingSource, "FirstName", true));
this.nameTextBox.DataBindings.Add(new Binding("Text", this.personBindingSource, "Name", true));
}
private List<Person> GetPersons()
{
return new List<Person>
{
new Person() { Name = "John", FirstName = "Doe" },
new Person() { Name = "Jane", FirstName = "Doe" },
new Person() { Name = "John", FirstName = "Smith" },
new Person() { Name = "Jane", FirstName = "Smith" },
};
}
public Person NewPerson()
{
_counter++;
return new Person() { FirstName = $"TATA {_counter}", Name = "TOTO" };
}
private void newPersonButton_Click(object sender, EventArgs e)
{
// Ici, on ajoute un nouvel élément à la liste source du BindingSource
// et pas au BindingSource lui-même. Le BindingSource se charge de mettre
// à jour la liste des éléments affichés dans le DataGridView.
// C'est possible car la liste source est implémente l'interface IBindingList<T>
Person person = NewPerson();
_persons.Add(person);
}
}
} |
Partager