ok I found the origin of the problem and I think a workaround too:
Your code is correct but you need to add a couple of things
- In the code of your form add a variable for the binding manager base:
BindingManagerBase bmb;
- Then after the code you have posted initialise your binding manager:
...
textBox2.DataBindings.Add("Text",dataView ,"COL2"); //also key field
bmb = This.BindingContext(yourDataView);
- Add one button to make the binded textboxes understand that you start a
new insertrion:
bmb.AddNew();
- Add another button to accept the data:
bmb.EndCurrentEdit();
- And to cancel adding data:
bmb.CancelCurrentEdit();
This works, but you need to do things with that binding manager. This is the
only way I found to correctely synchronize datagrid and other binded
controls. If anybody knows something simplier I'm intersted too
cnSoftware - 07 Oct 2004 16:56 GMT
well, what's the difference between BindingManagerBase and
CurrencyManager?Instead of
BindingManagerBase I have tried to use a currencymanager in my code, but the
result
is still far from my expectation.
UI of my program like this:
---------------------
| btnQuery | btnAdd |
---------------------
| |
| textBox1 | textBox2|
|--------------------|
| datagrid1 | // I only want to use the datagrid1 to display
the records while
---------------------- // I will actually input the data in textBox1 and
textBox2.
step 1. I click btnQuery to fill the dataSet:
{
dataSet = GetAllRows();
dataTable = dataSet.Tables[0];
dataView = dataTable.DefaultView;
textBox1.DataBindings.Add("Text",dataView,"Field1"); //Field1 is key
field
textBox2.DataBindings.Add("Text",dataView,"Field2");
datagrid1.DataSource = dataView;
_CurrencyManager = (CurrencyManager)this.BindingContext[dataView];
}
step 2. I click btnAdd to append a new row:
{
this._CurrencyManager.EndCurrentEdit();
this._CurrencyManager.AddNew();
textBox1.Focus(); //get ready for me to input data
}
Now there are several strange situations puzzle me:
1. If I set focus in textBox1 or textBox2 before step 2, everything is ok, a
new
row is appended at the end of the datagrid1 and the focus is in textBox1.
But
If I set focus in datagrid1 and then continue step 2, a new row is still
appended,
but when the focus jumps from the datagrid1 to textBox1, a warning
messagebox
appears to show,"Field1 could not be null,do you want to correct it?".
why does
it appear before I input data?
2. Continue the previous operation. If I choose "Yes" to correct, the focus
is set
to the new row of datagrid1 again. But if I choose "No", I can input data
in textBox1
and then textBox2. However, the new row of datagrid1 remains null value
before the
focus is set to the datagrid1 again. Since the data in textBox1 and the
datagrid1 have
been bound together through currencymanager, I think the modification of
data in textBox1 should
be shown in datagrid1 synchronously. So I can't explain the
situation.Why?
3. Furthur more, as I indicated before, since I just use the datagrid to
display records,
I set its Readonly property to be true. But when I click any cell of the
datagrid1,I find
it will still invoke the DataGridTextBoxColumn.Edit method. Why??
these problems have puzzled me for a long time, and I really appreciate any
advice.
Thanks a lot.
"Piotrek" <ileouddNOSPAMMERCi@hotmail.com>
> ok I found the origin of the problem and I think a workaround too:
> Your code is correct but you need to add a couple of things
[quoted text clipped - 25 lines]
> only way I found to correctely synchronize datagrid and other binded
> controls. If anybody knows something simplier I'm intersted too
cnSoftware - 07 Oct 2004 16:57 GMT
well, what's the difference between BindingManagerBase and
CurrencyManager?Instead of
BindingManagerBase I have tried to use a currencymanager in my code, but the
result
is still far from my expectation.
UI of my program like this:
---------------------
| btnQuery | btnAdd |
---------------------
| |
| textBox1 | textBox2|
|--------------------|
| datagrid1 | // I only want to use the datagrid1 to display
the records while
---------------------- // I will actually input the data in textBox1 and
textBox2.
step 1. I click btnQuery to fill the dataSet:
{
dataSet = GetAllRows();
dataTable = dataSet.Tables[0];
dataView = dataTable.DefaultView;
textBox1.DataBindings.Add("Text",dataView,"Field1"); //Field1 is key
field
textBox2.DataBindings.Add("Text",dataView,"Field2");
datagrid1.DataSource = dataView;
_CurrencyManager = (CurrencyManager)this.BindingContext[dataView];
}
step 2. I click btnAdd to append a new row:
{
this._CurrencyManager.EndCurrentEdit();
this._CurrencyManager.AddNew();
textBox1.Focus(); //get ready for me to input data
}
Now there are several strange situations puzzle me:
1. If I set focus in textBox1 or textBox2 before step 2, everything is ok, a
new
row is appended at the end of the datagrid1 and the focus is in textBox1.
But
If I set focus in datagrid1 and then continue step 2, a new row is still
appended,
but when the focus jumps from the datagrid1 to textBox1, a warning
messagebox
appears to show,"Field1 could not be null,do you want to correct it?".
why does
it appear before I input data?
2. Continue the previous operation. If I choose "Yes" to correct, the focus
is set
to the new row of datagrid1 again. But if I choose "No", I can input data
in textBox1
and then textBox2. However, the new row of datagrid1 remains null value
before the
focus is set to the datagrid1 again. Since the data in textBox1 and the
datagrid1 have
been bound together through currencymanager, I think the modification of
data in textBox1 should
be shown in datagrid1 synchronously. So I can't explain the
situation.Why?
3. Furthur more, as I indicated before, since I just use the datagrid to
display records,
I set its Readonly property to be true. But when I click any cell of the
datagrid1,I find
it will still invoke the DataGridTextBoxColumn.Edit method. Why??
these problems have puzzled me for a long time, and I really appreciate any
advice.
Thanks a lot.
"Piotrek" <ileouddNOSPAMMERCi@hotmail.com>
> ok I found the origin of the problem and I think a workaround too:
> Your code is correct but you need to add a couple of things
[quoted text clipped - 25 lines]
> only way I found to correctely synchronize datagrid and other binded
> controls. If anybody knows something simplier I'm intersted too