PC Review


Reply
Thread Tools Rate Thread

A complete example with bindings that doesn't work as expected

 
 
Tony Johansson
Guest
Posts: n/a
 
      13th Mar 2011
Hello!

A complete example where binding works in one directions only.
I have a DataGridView and when I click on the event handler
BtnCreateTextBox_Click a new TextBox is created.
and now I enter a name in the DataGridView cell and click enter and the name
is displayed in the TextBox which is perfect.
Now I do the same with three TextBoxes so I have three rows in the
datagridView which have the namnes
aaa
bbb
ccc
and each of these namnes is displayed in each TextBox.
Now I place the cursor on the last row in the DataGridView and change the
name aaa to 111 in the TextBox and change focus to the TextBox where the
name is bbb. The is no change at all in the DataGridView cell aaa. Now I
change bbb in the TextBox to 222 and change focus to the last textBox where
ccc is displayed and again no change at all in the dataGridView.
Now I change ccc in the TextBox to 333 and now I can see that ccc in the
DataGridView is changed to 333 because
the cursor was placed in this cell.

So why does it not work in both direction ?
I use this binding statement
textBox.DataBindings.Add("Text", thisPlayer, "Name",true,
DataSourceUpdateMode.OnPropertyChanged);
which should cause binding to work in both directions.



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 int xpos = 0;
private int ypos = 0;
private BindingSource bindingSource = new BindingSource();
private List<Player> players = new List<Player>();
private TextBox textBox;

private List<TextBox> textBoxes = new List<TextBox>();

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

private void BtnCreateTextBox_Click(object sender, EventArgs e)
{
textBox = new TextBox();
textBoxes.Add(textBox);

textBox.Location = new Point(xpos += 20, ypos += 20);
panel1.Controls.Add(textBox);
}

private void dataGridView1_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
if (textBox == null)
return;

Player thisPlayer = players[e.RowIndex];

foreach (TextBox tb in textBoxes)
{
if (tb.Text == thisPlayer.Name)
{
tb.DataBindings.Remove(tb.DataBindings["Text"]);
textBox = tb;
break;
}
}
textBox.DataBindings.Add("Text", thisPlayer, "Name",true,
DataSourceUpdateMode.OnPropertyChanged);
}
}

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();
((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;
this.dataGridView1.CellValueChanged += new
System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellValueChanged);
//
// 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;
}
}

//Tony



 
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
FontConverter doesn't work as expected Tony Johansson Microsoft C# .NET 7 18th Jun 2010 02:11 AM
A very small complete program that doesn't work as expected Tony Johansson Microsoft C# .NET 5 5th Jul 2009 12:00 AM
My FIRST Class!! But it doesn't work as expected - please HELP! Cap'n Ahab Microsoft VB .NET 36 2nd Jan 2006 12:58 PM
my network doesn't work as well as I expected =?Utf-8?B?VkxBRA==?= Windows XP Networking 2 5th Jan 2005 04:04 PM
photoshop 6.01 doesn't work as expected Kerstin Windows XP General 4 21st Sep 2004 06:33 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:05 PM.