Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > update TextBox bound to DataView
 

update TextBox bound to DataView

Good morning,

I have some TextBoxes bound to a DataView.  If the user makes any changes in the TextBoxes, I prompt to save or cancel before moving to another record.  Here is the code if the user chooses cancel:

else if (this.bolChangedAcct == true)
{
//Drop bindings
this.tabAcctMstrMaint_txtShortAccount.DataBindings.Clear();
this.tabAcctMstrMaint_txtAcctGroup.DataBindings.Clear();
this.tabAcctMstrMaint_cmbBalance.DataBindings.Clear();
this.tabAcctMstrMaint_cmbType.DataBindings.Clear();
this.tabAcctMstrMaint_txtDesc.DataBindings.Clear();

//Restore bindings
this.tabAcctMstrMaint_txtShortAccount.DataBindings.Add(new Binding("Text",dvAccounts,ACCTQUICK_FIELD));
this.tabAcctMstrMaint_txtAcctGroup.DataBindings.Add(new Binding("Text",dvAccounts,ACCTGROUP_FIELD));
this.tabAcctMstrMaint_cmbBalance.DataBindings.Add(new Binding("Text",dvAccounts,ACCTBALANCE_FIELD));
this.tabAcctMstrMaint_cmbType.DataBindings.Add(new Binding("Text",dvAccounts,ACCTTYPE_FIELD));
this.tabAcctMstrMaint_txtDesc.DataBindings.Add(new Binding("Text",dvAccounts,ACCTDESC_FIELD));


BuildAcct();
Update_tabAcctMstrMaint_dgAcctDetail();

GetInitialState();
this.bolChangedAcct = false;
this.ActiveControl = this.tabAcctMstrMaint_sbSearchBox;
}


I drop the bindings for the TextBoxes and then restore them.  Why don't the TextBoxes update thier text when the bingings are restored?  How do I do this?

Thanks
jmatt
MigrationUser 1  Wednesday, July 28, 2004 1:04 PM
One question, why are you dropping and adidng bindings?

You know you don't have to right?

Check out this simple app I did for databinding and navigating rows.

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2225&lngWId=10
MigrationUser 1  Wednesday, July 28, 2004 11:49 PM
Joe,

Thanks for your reply, I did download your code but I still have questions if you don't mind.
As a general matter;

1.  A form has a TextBox that is bound to a column in a Dataview.
2.  The user begins to change the text in the TextBox.
3.  The user has second thoughts and decides to click the "Cancel" button.

What has to happen so that the Cancel_Click event restores the original text as it appears in the underlying DataTable?

Dropping and recreating the bindings was my attempt to do this.

jmatt
MigrationUser 1  Thursday, July 29, 2004 12:14 PM
Data binding has what's called a currencymanager, it controls the state of the row/rows and when you call

me.bindingcontext(dataset,"tablename").cancelcurrentedit()

Then it will just lose the changes and resort back to it's previous value. this also works when adding new rows.

Every row in a data table has a rowstate (added, modified, unchanged, deleted and detatched). 

When you do a 
me.bindingcontext(dataset,"tablename").addnew

then input some values then decide you want to cancel you call 
me.bindingcontext(dataset,"tablename").cancelcurrentedit()

Now before saving (using the update of the data adapter) you should call
me.bindingcontext(dataset,"tablename").endcurrentedit()

This will close out the current edits and allow the data to be saved. You never want to call this and then call 
me.bindingcontext(dataset,"tablename").cancelcurrentedit()
because then the changes are set.

There is a whole world you need to explore, data bindings are a very large and complex animal but once you understand it, it can make the whole world open up to you.

You currently are doing way too much work.
MigrationUser 1  Thursday, July 29, 2004 3:05 PM
Joe,

I have the following:

DataView dvAccounts      
DataSet dsAccounts
DataTable dtAccounts
TextBox txtAccountDesc which is bound to the Acct_Description column in dvAccounts.

dvAccounts = new DataView(dtAccounts)

A user begins to edit txtAccountDesc and then hits "Cancel".

Cancel_Click event has

this.BindingContext[dsAccounts, dtAccounts.ToString()].CancelCurrentEdit();

The text in txtAccountDesc remains as the user edited and does not resort to original value.

Any ideas?  This state of affairs is what led me to try dropping and re-adding the bindings but that was out of desperation!

Your help is greatly appreciated!
jmatt
MigrationUser 1  Thursday, July 29, 2004 5:03 PM
that's because the Currencymanager depends on how it's bound, for instance if you bind it this way

textbox1.databindings.add(new binding("Text",dataset,"Tablename.ColumnName"))

It's a different currencymanager then

textbox1.databindings.add(new binding("Text",dataset.tables("TableName"),"ColumnName"))

Each one of these would require a different way to tackle the currencymanager

Method 1 (for the first one above) would look like.
me.bindingcontext(dataset,"TableName").cancelcurrentedit()

Method 2 would look like.
me.bindingcontext(dataset.tables("TableName")).cancelcurrentedit()

Now note that method 2 is the same as doing it this way too.
me.bindingcontext(datatable).cancelcurrentedit()

So depending on your bindings will depend on how you get the currencymanager which would then work if everything matches.

Hope this helps
MigrationUser 1  Thursday, July 29, 2004 5:26 PM
Thank you sir!  Got it to work.

If I can try your kindness with one more question:

If you have a form with a bunch of TextBoxes,  each bound to DataView a column,  how do you handle the creation of a new row?  I have a "New" button on my form that fires the DataRowView newRow = dvAccounts.AddNew() method.

This does not set the CurrancyManagers position to the new row however, at least not the way I am doing it,  but remains on the record current when "New" was clicked.  Therfore the user is really editing the current row rather than adding a new one.

Thanks
jmatt  
MigrationUser 1  Friday, July 30, 2004 1:12 PM
If you have taken the time to review the project I provided a link to you would know the answer to this question, which I also shown how to do an add new in a couple of my responses to date already.
MigrationUser 1  Friday, July 30, 2004 1:25 PM
You are correct, I was lazy.

I will study it this weekend.

jmatt
MigrationUser 1  Friday, July 30, 2004 1:44 PM
I don't mean to be harse, I just wrote that project to help people get a basic understanding of databinding, when I provide that link they can see how to move position and add, cancel and end edit. 

That way they can come back and ask questions on stuff they didn't understand. It was made to run using a SQL server, though you can look behind the buttons like the add button and see how things are done.

Hope this helps.
MigrationUser 1  Friday, July 30, 2004 3:35 PM
No problem you were not harsh,  I was lazy and deserved your response.

jmatt
MigrationUser 1  Friday, July 30, 2004 5:00 PM
Joe, is the DataSet search feature not implemented yet?  I see the search button opening up form 2, but I don't see any search feature

This would be extremely useful for Nata1
MigrationUser 1  Friday, July 30, 2004 5:13 PM
What the sample does is open the form with a grid with all the records, hence the user would find it in the grid and double click and the first form would change position. 

To do actural searches, you would have to emply an engine if you wanted to do more then one column at a time. I personally use the Component One True Datagrid which has a option to show text boxes at the top and you enter in info to filter each column. You could do the same with a datagrid and the default view of the table to use the rowfilter to filter it.

Hope this helps.
MigrationUser 1  Friday, July 30, 2004 9:57 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView Help
• Finding a text in a TREEVIEW
• DataGridView CellClick event is firing twice
• Insert data from Data grid view to database through the use of Datatable
• Data not updated unless controls loose focus first.
• Database Help?
• Datagridview Performance "Work around"
• application design with unreliable/periodic connection to sql server
• Binding a GridView to a different datasource
• How do I delete a datarow from a databound listbox?