Binding to the same TextBox and record index in a DataGridView until enter is hit


T

Tony Johansson

This is a complete program.
I have bound the DataGridView.DataSource to BindingList<Player> as you can
see in the code.
In this program I click a Button so event handle BtnCreateTextBox_Click is
called and a new TextBox is created and
put in the form. I subscribe to the event textBox_TextChanged so this event
handler is called for each change in the textbox..
When I start to write in the textBox it doesn't work the way that I want and
this is understandable.
If I write the word test the following is displayed in the DataGridView.
When I have written t this t is also displayed in the first
row in the DataGridView.Now I enter e in the textbox and this e is now
displayed on the second row in the DataGridView:
Now I enter s in the textbox and this s is now displayed on the third row in
the DataGridView.
Now I enter t in the textbox and this t is now displayed on the fourth row
in the DataGridView:
As I mentioned this is obvious because I add a new player to the collection
for each call to the textBox_TextChanged

Now to my question how do I do if I want to keep writing to the same record
index in the DataGridView until I hit the enter key in the TextBox.

namespace Testing
{
public partial class Form1 : Form
{
private BindingSource bindingSource = new BindingSource();
private BindingList<Player> players = new BindingList<Player>();
private TextBox textBox;

public Form1()
{
InitializeComponent();
bindingSource.DataSource = players;
dataGridView1.DataSource = bindingSource;
}

private void BtnCreateTextBox_Click(object sender, EventArgs e)
{
textBox = new TextBox();
textBox.TextChanged += new EventHandler(textBox_TextChanged);
panel1.Controls.Add(textBox);
}

private void textBox_TextChanged(object sender, EventArgs e)
{
Player thisPlayer = new Player();
TextBox tb = (TextBox)sender;
thisPlayer.Name = tb.Text;
players.Add(thisPlayer);
}
}

public class Player
{
private int playerID;
private string name;
private int count;

public int Count
{
get { return count; }
set { count = value; }
}

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; }
}
}
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();
((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;
//
// 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.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;
}

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());
}
}
}
 
Ad

Advertisements

T

Tony Johansson

I have solved it.

//Tony

Tony Johansson said:
This is a complete program.
I have bound the DataGridView.DataSource to BindingList<Player> as you
can see in the code.
In this program I click a Button so event handle BtnCreateTextBox_Click is
called and a new TextBox is created and
put in the form. I subscribe to the event textBox_TextChanged so this
event handler is called for each change in the textbox..
When I start to write in the textBox it doesn't work the way that I want
and this is understandable.
If I write the word test the following is displayed in the DataGridView.
When I have written t this t is also displayed in the first
row in the DataGridView.Now I enter e in the textbox and this e is now
displayed on the second row in the DataGridView:
Now I enter s in the textbox and this s is now displayed on the third row
in the DataGridView.
Now I enter t in the textbox and this t is now displayed on the fourth row
in the DataGridView:
As I mentioned this is obvious because I add a new player to the
collection for each call to the textBox_TextChanged

Now to my question how do I do if I want to keep writing to the same
record index in the DataGridView until I hit the enter key in the TextBox.

namespace Testing
{
public partial class Form1 : Form
{
private BindingSource bindingSource = new BindingSource();
private BindingList<Player> players = new BindingList<Player>();
private TextBox textBox;

public Form1()
{
InitializeComponent();
bindingSource.DataSource = players;
dataGridView1.DataSource = bindingSource;
}

private void BtnCreateTextBox_Click(object sender, EventArgs e)
{
textBox = new TextBox();
textBox.TextChanged += new EventHandler(textBox_TextChanged);
panel1.Controls.Add(textBox);
}

private void textBox_TextChanged(object sender, EventArgs e)
{
Player thisPlayer = new Player();
TextBox tb = (TextBox)sender;
thisPlayer.Name = tb.Text;
players.Add(thisPlayer);
}
}

public class Player
{
private int playerID;
private string name;
private int count;

public int Count
{
get { return count; }
set { count = value; }
}

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; }
}
}
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();

((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;
//
// 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.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;
}

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());
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top