Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / October 2004

Tip: Looking for answers? Try searching our database.

two issues on data binding

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
cnSoftware - 24 Sep 2004 16:05 GMT
Hi,
 I have tow problems when I am writing a win-form program.
The First is:
On a form, I place two textboxes, two buttons and a dataGrid.
Then I bind them to a dataView like this:
 ...
 dataView = dataSet.Tables[0].DefaultView;
 dataGrid.DataSource = dataView;
 textBox1.DataBindings.Add("Text",dataView ,"COL1"); //key field
 textBox2.DataBindings.Add("Text",dataView ,"COL2"); //also key field
 ...

then I query the db server and fill the dataset.Both the textboxes
and the datagrid display the result.

After that, I click the button1 to add a new row to the dataView:
 dataRowView = this.dataView .AddNew();
The problem raises. A new Line is appended to the end of the dataGrid.
But its cursor remains at the first record and the textboxes also show
the fields of the first row.

Should I miss something?

The second problem is that:
I move the cursor manully to the new added line in the dataGrid so I can
append a new record. I input something in the textbox1 and then in textBox2.
Right after I input something in textBox2, I directly click the button2 to
save the new record.But a exception is thrown,shows that "COL2" should not
be a null value. It seems that if I click the button2 immediately after I
input in textBox2 without an explicit focus lost of textBox2,the text
of the textBox2 will not be assigned to "COL2" of the row.

How to handle this problem?

I am really need your help,friends.
Thanx.

Yours sincerely
LEO
bookguru@126.com
Piotrek - 25 Sep 2004 10:42 GMT
I can't give you a solution just an explaination, hope it will help you

> On a form, I place two textboxes, two buttons and a dataGrid.
> Then I bind them to a dataView like this:
[quoted text clipped - 15 lines]
>
> Should I miss something?

I had this behavior when I made my own custom collection and tried to bind
It,
it occurs when in that collection I forgot to fire ListChanged event,
specifying in arguments that a new record has been added

> The second problem is that:
> I move the cursor manully to the new added line in the dataGrid so I can
[quoted text clipped - 6 lines]
>
> How to handle this problem?

hum I think there is a mess between dataview and its datatable, which is
really hosting the data:
try to keep them bound together. I think you buit that dataview manually
outside the datatable: the probleme must be there, somewhere..
cnSoftware - 26 Sep 2004 15:34 GMT
thanks

> I can't give you a solution just an explaination, hope it will help you
>
[quoted text clipped - 39 lines]
> try to keep them bound together. I think you buit that dataview manually
> outside the datatable: the probleme must be there, somewhere..
Piotrek - 26 Sep 2004 20:41 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.