T
Tony Johansson
Hello!
This is a program that contains one DataGridView and two Buttons.
The program have the usual Form1 class and one Player class.
This Player class is bound to the DataGridView like this
private BindingSource bindingSource = new BindingSource();
private List<Player> players = new List<Player>();
bindingSource.DataSource = players;
dataGridView1.DataSource = bindingSource;
All this code is of cource included in this code listing because it's a
complete program.
The two buttons is called BtnCreateTextBox and BtnBindToTextBox.
So the following steps is that I follow.
1.Click on the BtnCreateTextBox. This will create a TextBox and put this
into the panel control. This works fine.
2.Enter a name into the DataGridView.
3.Click on the BtnBindToTextBox. This should display the entered name in the
created TextBox. This works fine
4 Go to 1 and create a new TextBox and enter a new name on a new row in the
DataGridView and click on the BtnBindToTextBox to bin the new name to the
new TextBox that you created
As you can see is that I want to bind each entered name in the DataGridView
to a TextBox that is created when I click on the BtnCreateTextBox.
These steps above 1 to 3 works fine for the first time but if I create a
second TextBox and enter a second name in the DataGridView and click on the
BtnBindToTextBox it doesn't work as I want.
//Complete program listing
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Testing
{
public partial class Form1 : Form
{
private BindingSource bindingSource = new BindingSource();
private List<Player> players = new List<Player>();
private int xpos = 0;
private int ypos = 0;
private TextBox textBox;
public Form1()
{
InitializeComponent();
bindingSource.DataSource = players;
dataGridView1.DataSource = bindingSource;
}
private void BtnCreateTextBox_Click(object sender, EventArgs e)
{
textBox = new TextBox();
textBox.Location = new Point(xpos += 20, ypos += 20);
panel1.Controls.Add(textBox);
}
private void BtnBindToTextBox_Click(object sender, EventArgs e)
{
textBox.DataBindings.Add("text", bindingSource, "Name");
}
}
public class Player
{
private int playerID;
private string name;
public Player(int ID, string name)
{
this.playerID = ID;
this.name = name;
}
public Player()
{}
public string Name
{
get { return name; }
set { name = value; }
}
public int PlayerID
{
get { return playerID; }
set { playerID = value; }
}
}
//A copy paste from Form1.Designer.cs
partial class Form1
{
/// <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()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.BtnCreateTextBox = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.BtnBindToTextBox = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 137);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(267, 121);
this.dataGridView1.TabIndex = 0;
//
// BtnCreateTextBox
//
this.BtnCreateTextBox.Location = new System.Drawing.Point(285,
167);
this.BtnCreateTextBox.Name = "BtnCreateTextBox";
this.BtnCreateTextBox.Size = new System.Drawing.Size(153, 23);
this.BtnCreateTextBox.TabIndex = 1;
this.BtnCreateTextBox.Text = "CreateTextBox";
this.BtnCreateTextBox.UseVisualStyleBackColor = true;
this.BtnCreateTextBox.Click += new
System.EventHandler(this.BtnCreateTextBox_Click);
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(12, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(405, 108);
this.panel1.TabIndex = 2;
//
// BtnBindToTextBox
//
this.BtnBindToTextBox.Location = new System.Drawing.Point(285,
226);
this.BtnBindToTextBox.Name = "BtnBindToTextBox";
this.BtnBindToTextBox.Size = new System.Drawing.Size(153, 23);
this.BtnBindToTextBox.TabIndex = 3;
this.BtnBindToTextBox.Text = "BindDGVNameToTextBox";
this.BtnBindToTextBox.UseVisualStyleBackColor = true;
this.BtnBindToTextBox.Click += new
System.EventHandler(this.BtnBindToTextBox_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(444, 270);
this.Controls.Add(this.BtnBindToTextBox);
this.Controls.Add(this.panel1);
this.Controls.Add(this.BtnCreateTextBox);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button BtnCreateTextBox;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button BtnBindToTextBox;
}
//A copy paste from program.cs
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
This is a program that contains one DataGridView and two Buttons.
The program have the usual Form1 class and one Player class.
This Player class is bound to the DataGridView like this
private BindingSource bindingSource = new BindingSource();
private List<Player> players = new List<Player>();
bindingSource.DataSource = players;
dataGridView1.DataSource = bindingSource;
All this code is of cource included in this code listing because it's a
complete program.
The two buttons is called BtnCreateTextBox and BtnBindToTextBox.
So the following steps is that I follow.
1.Click on the BtnCreateTextBox. This will create a TextBox and put this
into the panel control. This works fine.
2.Enter a name into the DataGridView.
3.Click on the BtnBindToTextBox. This should display the entered name in the
created TextBox. This works fine
4 Go to 1 and create a new TextBox and enter a new name on a new row in the
DataGridView and click on the BtnBindToTextBox to bin the new name to the
new TextBox that you created
As you can see is that I want to bind each entered name in the DataGridView
to a TextBox that is created when I click on the BtnCreateTextBox.
These steps above 1 to 3 works fine for the first time but if I create a
second TextBox and enter a second name in the DataGridView and click on the
BtnBindToTextBox it doesn't work as I want.
//Complete program listing
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Testing
{
public partial class Form1 : Form
{
private BindingSource bindingSource = new BindingSource();
private List<Player> players = new List<Player>();
private int xpos = 0;
private int ypos = 0;
private TextBox textBox;
public Form1()
{
InitializeComponent();
bindingSource.DataSource = players;
dataGridView1.DataSource = bindingSource;
}
private void BtnCreateTextBox_Click(object sender, EventArgs e)
{
textBox = new TextBox();
textBox.Location = new Point(xpos += 20, ypos += 20);
panel1.Controls.Add(textBox);
}
private void BtnBindToTextBox_Click(object sender, EventArgs e)
{
textBox.DataBindings.Add("text", bindingSource, "Name");
}
}
public class Player
{
private int playerID;
private string name;
public Player(int ID, string name)
{
this.playerID = ID;
this.name = name;
}
public Player()
{}
public string Name
{
get { return name; }
set { name = value; }
}
public int PlayerID
{
get { return playerID; }
set { playerID = value; }
}
}
//A copy paste from Form1.Designer.cs
partial class Form1
{
/// <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()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.BtnCreateTextBox = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.BtnBindToTextBox = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 137);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(267, 121);
this.dataGridView1.TabIndex = 0;
//
// BtnCreateTextBox
//
this.BtnCreateTextBox.Location = new System.Drawing.Point(285,
167);
this.BtnCreateTextBox.Name = "BtnCreateTextBox";
this.BtnCreateTextBox.Size = new System.Drawing.Size(153, 23);
this.BtnCreateTextBox.TabIndex = 1;
this.BtnCreateTextBox.Text = "CreateTextBox";
this.BtnCreateTextBox.UseVisualStyleBackColor = true;
this.BtnCreateTextBox.Click += new
System.EventHandler(this.BtnCreateTextBox_Click);
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(12, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(405, 108);
this.panel1.TabIndex = 2;
//
// BtnBindToTextBox
//
this.BtnBindToTextBox.Location = new System.Drawing.Point(285,
226);
this.BtnBindToTextBox.Name = "BtnBindToTextBox";
this.BtnBindToTextBox.Size = new System.Drawing.Size(153, 23);
this.BtnBindToTextBox.TabIndex = 3;
this.BtnBindToTextBox.Text = "BindDGVNameToTextBox";
this.BtnBindToTextBox.UseVisualStyleBackColor = true;
this.BtnBindToTextBox.Click += new
System.EventHandler(this.BtnBindToTextBox_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(444, 270);
this.Controls.Add(this.BtnBindToTextBox);
this.Controls.Add(this.panel1);
this.Controls.Add(this.BtnCreateTextBox);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button BtnCreateTextBox;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button BtnBindToTextBox;
}
//A copy paste from program.cs
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}