PC Review


Reply
Thread Tools Rate Thread

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

 
 
Tony Johansson
Guest
Posts: n/a
 
      31st Mar 2011
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());
}
}
}


 
Reply With Quote
 
 
 
 
Tony Johansson
Guest
Posts: n/a
 
      1st Apr 2011
I have solved it.

//Tony

"Tony Johansson" <(E-Mail Removed)> skrev i meddelandet
news:in1qus$ac3$(E-Mail Removed)...
> 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());
> }
> }
> }
>
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGridView: Need to make Enter key create newline in textbox Steve K Microsoft C# .NET 3 28th Oct 2008 08:22 AM
Binding List<T> to a DataGridView = DataGridView Empty admlangford@gmail.com Microsoft C# .NET 3 1st Oct 2008 07:59 AM
VB 2005 Row index in datagridview <> row index in dataset Robert Koller Microsoft VB .NET 7 2nd Jun 2005 02:52 AM
threading, what different between DataGrid Binding to Binding TextBox mttc Microsoft Dot NET Framework Forms 3 19th Nov 2004 08:33 AM
Binding a textbox to a single record in a table. Andy Microsoft Access Reports 2 4th Oct 2004 05:16 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:55 AM.