Adding new rows to a BindingSource?
-
I'm new to ADO.NET so if this question is odd, forgive me. Anyways, here the problem. I have a DataSet, that I populated with a SqlDataAdapter that I made, then I binded the DataSet to a BindingSource and hooked that up to some controls on my Form. Now if I want to add a new record to the DataSet table (there's only one in it) do I do it through the BindingSource or through the DataSet? And once the new row is added will the BindingSource be able to see it right away (so the Form can display the new record) or do I have to something to update the BindingSource? If a quick example could be provided as well that would be awesome. Thanks for any and all help.
- Aaron
-
I'm new to ADO.NET so if this question is odd, forgive me. Anyways, here the problem. I have a DataSet, that I populated with a SqlDataAdapter that I made, then I binded the DataSet to a BindingSource and hooked that up to some controls on my Form. Now if I want to add a new record to the DataSet table (there's only one in it) do I do it through the BindingSource or through the DataSet? And once the new row is added will the BindingSource be able to see it right away (so the Form can display the new record) or do I have to something to update the BindingSource? If a quick example could be provided as well that would be awesome. Thanks for any and all help.
- Aaron
The AddNew method of the bindingsource control will achieve what you are looking for. You can then enter the data for the new record in the bound controls. You will need a Save button and a Cancel button. The save button should validate the data entered by the user, call the EndEdit method of the binding source, and finally call the Update method of the tableadapter to write the new record to the datasource. The Cancel button should call the CancelEdit method of the bindingsource to discard any changes made. Whether or not you need to do anything else depends on a few things in your program structure. For example, whether you are updating the dataset from the same thread as your UI. If you do not see the results of the changes immediately, you may need to call the ResetBindings method of the bindingsource. Hope this helps.
-
The AddNew method of the bindingsource control will achieve what you are looking for. You can then enter the data for the new record in the bound controls. You will need a Save button and a Cancel button. The save button should validate the data entered by the user, call the EndEdit method of the binding source, and finally call the Update method of the tableadapter to write the new record to the datasource. The Cancel button should call the CancelEdit method of the bindingsource to discard any changes made. Whether or not you need to do anything else depends on a few things in your program structure. For example, whether you are updating the dataset from the same thread as your UI. If you do not see the results of the changes immediately, you may need to call the ResetBindings method of the bindingsource. Hope this helps.
Sorry about the late reply, but I haven't been near my computer for some time. Anyways, your answer worked perfectly so thank you very much.
- Aaron