Update UI with DS Changes

  • Thread starter Thread starter Knut Vonheim
  • Start date Start date
K

Knut Vonheim

I programmatically change the values in a dataset. However, the changes are
not reflected in the UI. What am I missing.

If I set the values in the UI controls the dataset is updated as expected.
However, I am trying to decouple the logic from the UI (in case we switch
from forms to ASP etc).

Any help, hints, or comments are greatly appreciated.

Cheers,

Knut Vonheim
 
I can simplify the question by asking will setting values in the dataset
update the bound controls in the UI?

Or do I need to call a member function on the data set or control?

My code currently sets new values in the UI controls. However, I would like
avoid referencing the UI controls in my program logic.

Thanks again,
Knut Vonheim
 
Not sure what you mean... Create a new VB PRoject and add a textbox and button to the form and paste in the following code.. As soon as the valyue changes teh textbox is
updated...

Dim con As New System.Data.SqlClient.SqlConnection("server=YOURSERVER;uid=sa;pwd=YOURPWD;database=northwind")
Dim daCust As New System.Data.SqlClient.SqlDataAdapter("Select * From Customers Where CustomerID Like 'A%'", con)
Dim ds As New DataSet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
daCust.Fill(ds, "Cust")
daCust.MissingSchemaAction = MissingSchemaAction.AddWithKey
TextBox1.DataBindings.Add("Text", ds, "cust.CompanyName")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dr As DataRow
dr = ds.Tables("Cust").Rows(0)
dr.BeginEdit()
dr.Item("CompanyName") = "Test"
dr.EndEdit()
If dr.RowState = DataRowState.Modified Then
ds.Tables("Cust").LoadDataRow(dr.ItemArray, False)
ElseIf dr.RowState = DataRowState.Detached Then
ds.Tables("Cust").Rows.Add(dr)
End If
End Sub

Want to know more? Check out the MSDN Library at http://msdn.microsoft.com or the Microsoft Knowledge Base at http://support.microsoft.com

Scot Rose, MCSD
Microsoft Visual Basic Developer Support
Email : (e-mail address removed) <Remove word online. from address>

This posting is provided “AS IS”, with no warranties, and confers no rights.




--------------------
 
Scot,

I found the CurrencyManager actually manages the binding contexts. I thought
for a long time it was primarily a "navigation tool" rather than a binding
context manager.

Hence, when I call myCM.Refresh() everything gets updated. My confusion was
based on my earlier thinking of not needing a CM since I only retrieved one
row at a time.

Thanks for the DataRow.BeginEdit() example! I am looking into this next.

Cheers,
Knut
Scot Rose said:
Not sure what you mean... Create a new VB PRoject and add a textbox and
button to the form and paste in the following code.. As soon as the valyue
changes teh textbox is
updated...

Dim con As New System.Data.SqlClient.SqlConnection("server=YOURSERVER;uid=sa;pwd=YOURPWD;da
tabase=northwind")
Dim daCust As New System.Data.SqlClient.SqlDataAdapter("Select * From
Customers Where CustomerID Like 'A%'", con)
Dim ds As New DataSet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
daCust.Fill(ds, "Cust")
daCust.MissingSchemaAction = MissingSchemaAction.AddWithKey
TextBox1.DataBindings.Add("Text", ds, "cust.CompanyName")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim dr As DataRow
dr = ds.Tables("Cust").Rows(0)
dr.BeginEdit()
dr.Item("CompanyName") = "Test"
dr.EndEdit()
If dr.RowState = DataRowState.Modified Then
ds.Tables("Cust").LoadDataRow(dr.ItemArray, False)
ElseIf dr.RowState = DataRowState.Detached Then
ds.Tables("Cust").Rows.Add(dr)
End If
End Sub

Want to know more? Check out the MSDN Library at http://msdn.microsoft.com
or the Microsoft Knowledge Base at http://support.microsoft.com
 
Back
Top