T
Tony Johansson
Hello!
This two-way binding works in one project but not in the other despide using
this DataSourceUpdateMode.OnPropertyChanged.
The only difference between these two project is that in one we have a
simple TextBox and in the other we have a user control where we use the
TextBox in this user control.
In the project where two way binding works I have this code see below and as
you can see I just changed this row
textBox.DataBindings.Add("Text", thisPlayer, "Name");
to this row
textBox.DataBindings.Add("Text", thisPlayer, "Name",true,
DataSourceUpdateMode.OnPropertyChanged);
So here I enter a name in a DataGridView cell push the enter key and the
name is displayed in the TextBox. Now I can change the name in the TextBox
and push the enter key and the name is changed in the DataGridView cell.
Now in the project where I have a user control that contains several control
but one of these is a TextBox.
Now it works perfect in one directions so if I enter a name in a
DataGridView cell and push the enter key this name is displayed in the user
control TextBox if I now change this name in the user control this name is
not change in the DataGridView cell but if I just click in the corresponding
cell in the DataGridView the name is updated to match what I wrote in the
user control.
All code for the binding is listed.
So my question is why does it not work in both directions when I use a user
control ?
// This code is for the project where two way binding works
//*********************************************
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);
}
// This code is for the project where two way binding doesn't work
//*************************************************
private void dgvAllPlayers_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
if (pg != null)
{
Player thisPlayer = gameManager.Players[e.RowIndex];
foreach (PlayerGUI pGUI in gameManager.PlayerGUIs)
{
if (pGUI.Namn == thisPlayer.Name)
{
pGUI.DataBindings.Remove(pGUI.DataBindings["Namn"]);
pg = pGUI;
if (string.Equals(thisPlayer.Name, Croupier,
StringComparison.CurrentCultureIgnoreCase))
BtnStartGame.Enabled = true;
break;
}
}
if (gameManager.Players.Count <= gameManager.PlayerGUIs.Count)
pg.DataBindings.Add("Namn", thisPlayer, "Name", true,
DataSourceUpdateMode.OnPropertyChanged);
}
}
//Tony
This two-way binding works in one project but not in the other despide using
this DataSourceUpdateMode.OnPropertyChanged.
The only difference between these two project is that in one we have a
simple TextBox and in the other we have a user control where we use the
TextBox in this user control.
In the project where two way binding works I have this code see below and as
you can see I just changed this row
textBox.DataBindings.Add("Text", thisPlayer, "Name");
to this row
textBox.DataBindings.Add("Text", thisPlayer, "Name",true,
DataSourceUpdateMode.OnPropertyChanged);
So here I enter a name in a DataGridView cell push the enter key and the
name is displayed in the TextBox. Now I can change the name in the TextBox
and push the enter key and the name is changed in the DataGridView cell.
Now in the project where I have a user control that contains several control
but one of these is a TextBox.
Now it works perfect in one directions so if I enter a name in a
DataGridView cell and push the enter key this name is displayed in the user
control TextBox if I now change this name in the user control this name is
not change in the DataGridView cell but if I just click in the corresponding
cell in the DataGridView the name is updated to match what I wrote in the
user control.
All code for the binding is listed.
So my question is why does it not work in both directions when I use a user
control ?
// This code is for the project where two way binding works
//*********************************************
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);
}
// This code is for the project where two way binding doesn't work
//*************************************************
private void dgvAllPlayers_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
if (pg != null)
{
Player thisPlayer = gameManager.Players[e.RowIndex];
foreach (PlayerGUI pGUI in gameManager.PlayerGUIs)
{
if (pGUI.Namn == thisPlayer.Name)
{
pGUI.DataBindings.Remove(pGUI.DataBindings["Namn"]);
pg = pGUI;
if (string.Equals(thisPlayer.Name, Croupier,
StringComparison.CurrentCultureIgnoreCase))
BtnStartGame.Enabled = true;
break;
}
}
if (gameManager.Players.Count <= gameManager.PlayerGUIs.Count)
pg.DataBindings.Add("Namn", thisPlayer, "Name", true,
DataSourceUpdateMode.OnPropertyChanged);
}
}
//Tony