Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bound objects and datarow.addnew 1

Status
Not open for further replies.

misterstick

Programmer
Apr 7, 2000
633
GB
forgive the confusion, but i'm suffering paradigm breakdown. this isn't a problem with c# as such, more that i can't see how to do something. if someone could explain it to me (slowly for preference) i'd be very grateful.

i have a form with a dataset on it.
the controls on the form are bound to a table within the dataset.

in order to add a new record, msdn suggests you create a new row of the datatable and add it to the rows collection.

my problem is this.

i can't add the new row to the rows collection because i haven't prompted the user for data to fill it with, and all the columns are not null.

the controls are bound to the table in the dataset and not the new row object, which isn't in the rows collection of the table because i haven't added it yet.

i don't want to bind the controls to the new row for the duration of the update because that would be just dumb.

what am i missing?


mr s. <;)

 
Hey, I'm a little confused....do you have a datagrid on the form? Can you give a little more explanation about how the user is supposed to add records to the table?? That may help come up with a solution.

Good luck,
Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
Are you trying to add a new row for the DataTable from a control( say TextBox) and the user should enter the new data to the control before it update the new row.?


If this is the case- Clear the the text fileds in the TextBoxs(control)
make sure the TextBox is not null before update it.


DataRow newRow = dataTable.NewRow( );
newRow["CustomerID"] = txtCompanyID.Text;
...
// add the new row to the table

dataTable.Rows.Add(newRow);

// update the database
try
{
// update the customer table(dataset)
dataAdapter.Update(dataSet,"Customers");
dataSet.AcceptChanges( );

// let user know it
lblMessage.Text = "Updated!";
Application.DoEvents( );


// clear all the text fields

}
catch (SqlException ex)
{
dataSet.RejectChanges( );
MessageBox.Show(ex.Message);
}
 

....do you have a datagrid on the form

no, there are only text boxes to edit the data.

If this is the case- Clear the the text fileds in the TextBoxs(control)
make sure the TextBox is not null before update it.

so i should forget that the controls are bound to the datatable?

1. clear the dataset.rows collection
2. let the user enter data
3. explicitly set the columns to the value of the requisite text box when i want to update?

doesn't this mean duplicating the replationships between the columns and the textboxes: once in the databound property, and once in code?


mr s. <;)

 
any shame i might feel at having to code things twice (and it's not as if my first attempt at a data entry screen isn't klugy enough as it is) is forgotten in the rush of pure pleasure i just got when the suggested solution worked.

if i could give you two stars i would.

many thanks.


mr s. <;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top