Data Binding

Y

YK Ng

Hi,

I retrieve a customer row from the Northwind database
into a DataSet and bind it to a number of text boxes as
follows:

private void BindData(string custID)
{
m_dsCust = m_custDAL.Load(custID);

txtCompanyName.DataBinding.Add(
"Text", m_dsCust, "Customer.CompanyName");
txtContactName.DataBinding.Add(
"Text", m_dsCust, "Customer.ContactName");
txtContactTitle.DataBinding.Add(
"Text", m_dsCust, "Customer.ContactTitle");
}

Next, I changed the company name by typing some text into
the bound text box (txtCompanyName). Then, I click a
button to retrieve the changes that I made and attempt to
update it as follows:

private void Button1_Click(object sender, EventArgs e)
{
DataSet changes =
m_dsCust.GetChanges(DataRowState.Modified);

m_custDAL.Save(changes);
}

I supposed the GetChanges() should return a DataSet with
the "Customer" table and a row (i.e., the only row I
retrieved) because I changed its Company Name. However, I
get "null" from GetChanges().

Am I doing anything wrong? Or, is there something hidden
I don't know?

Please advise.
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi,

From what I know, new data are pushed back to the data source when the
control looses focus. However, when a button is clicked, a control does not
loose focus. Therefore, before querying the data set for changes, you will
have to force the Data Binding mechanics to push updated values back to the
data source.

To do so, you can obtain the corresponding currency manager object from the
form's BindingContext property and call its EndCurrentEdit method.
 
D

Dmitriy Lapshin [C# / .NET MVP]

Umm...strange, this works for DataGrids for sure. Well, then you may just
try to move focus off the control programmatically when the user clicks the
button. You see, I haven't had any significant experience with binding to
anything other than DataGrids, so I am just guessing here in hope this might
help in the end.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top