We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to prevent bound textbox updating cell in datagrid ?

I have a datagrid bound to a datatable. I also have a text box that uses DataBindings.Add to bind to a column in the datatable. When I move rows in the grid the text box tracks the change and displays text from the current grid row for the bound column. If I edit the text in the text box the change is mirrored in the datagrid. Does anyone know how I can prevent changes to the text box altering the text in the grid ? I've been trying to do this using DataRowVersion without luck, the only other way I can think of is to forget databinding and track row changes in the grid and read the value of a cell - which means writing alot more code :(

3 Replies

NI Nic May 16, 2003 03:45 PM UTC

Try setting TextBox.ReadOnly=True.

CH Chris May 19, 2003 06:08 AM UTC

Nic, unfortunately setting the text box as read only prevents users typing into it, however, the text box is used to enter a search string. When a user clicks a row in the grid the text box is auto populated (via its databinding) with data from the selected grid row. The user can 'tweak' this string before running the search. What I'm after is a way to get the grid to display the original row version, rather than the current version, or a way to undo / rollback any changes the user makes in the text box.

NI Nic May 25, 2003 11:26 AM UTC

If you say that DataRowVersion doesn't work for you,try with DataBinding events Format & Parse. In code part where you bind TextBox1 to DataSource, add handler for those events: TextBox1.Databidings.Add("Text", _ dsTmp.Table1, "Column1Name") .... AddHandler TextBox1.DataBindings(0).Format, _ AddressOf FromDataTable AddHandler TextBox1.DataBindings(0).Parse, _ AddressOf ToDataTable Private Sub ToDataTable(ByVal sender As _ Object, ByVal e As ConvertEventArgs) Try e.Value = originalString Catch End Try End Sub Private Sub FromDataTable(ByVal sender As _ Object, ByVal e As ConvertEventArgs) Try originalString = CStr(e.Value) Catch End Try End Sub On Class level declare: Dim originalString as String="" Only downside is in dsTmp.GetChanges. Format event changes Column1 in current DataRow, but only those DataRows where user modified text in TextBox1. Hope this helps Nic

Live Chat Icon For mobile
Up arrow icon