How to save textbox.text to the dataset when it's bound to datagri

G

Guest

Hi, I have a window based program. One of the form has several textboxes and
a datagrid. The textboxes are bind to the same dataset table as the datagrid
and the text changes to reflect different row selected in the datagrid.

I want to save the changes that user make in the textboxes when they select
a different row in the datagrid. I tried capturing the textbox.text at
datagrid's CurrentCellChanged event but by then the textbox.text is lost
already becuase the binding is to the new currently selected row.

private void VListing_CurrentCellChanged(object sender, System.EventArgs e)
{
if(Changed)
{
dsVehicle.Tables["vehDetail"].Rows[prevRow]["Model"] = test;
prevRow = VListing.CurrentCell.RowNumber;
UpdateDataSet(dsVehicle);
}
Changed = false;
}

private void txtVModel_Validated(object sender, System.EventArgs e)
{
test = txtVModel.Text.ToString();

}
I tried capturing the textbox.text at textbox validated event and then
assign that to the dataset.tables.rows[col] and then update the database but
that isn't working either. I did capter the textbox.text but the assignment
to the dataset and update to the database is not working. Can someone tell
me what's the best way to do this? Or point me to an example?



Thanks, Alpha
 
D

Dmytro Lapshyn [MVP]

Hi Alpha,

My understanding is that if you change the text in a bound text box and then
move the input focus elsewhere, the change must be automatically propagated
to the underlying data source. So your ultimate goal might be how to force
the text box to lose input focus when the grid is being clicked (if that
does not happen automatically).
 
G

Guest

The form has several textboxes on the top half and the bottom has a datagrid.
The textboxes are bind to the same dataset table as the datagrid so that
when user selects different row in the DG the textboxes would display the
data related to that row.

I want to save the changes that users make to the textboxes when they either
click on another row on the DG or click OK button. When I use the following
code doesn't it change the dataset table for that row?
dsVehicle.Tables["vehDetail"].Rows[prevRow]["Model"] = test;

I then call Updatedataset() to update the database. But none of this is
working.

private void UpdateDataSet(DataSet myDataSet)
{
// Check for changes with the HasChanges method first.
if(!myDataSet.HasChanges(DataRowState.Modified)) return;
// Create temporary DataSet variable.
DataSet xDataSet;
// GetChanges for modified rows only.
xDataSet = myDataSet.GetChanges(DataRowState.Modified);
// Check the DataSet for errors.
if(xDataSet.HasErrors)
{
MessageBox.Show("There is an error getting the changed data.", "VMS -
Cycle item error.", MessageBoxButtons.OK);
}
else
{
// After fixing errors, update the data source with the DataAdapter
// used to create the DataSet.
// if (bool oleDbConnection1.Close())
sdaVehicle.Update(dsVehicle, "VehDetail");
}
}



Dmytro Lapshyn said:
Hi Alpha,

My understanding is that if you change the text in a bound text box and then
move the input focus elsewhere, the change must be automatically propagated
to the underlying data source. So your ultimate goal might be how to force
the text box to lose input focus when the grid is being clicked (if that
does not happen automatically).

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


Alpha said:
Hi, I have a window based program. One of the form has several textboxes
and
a datagrid. The textboxes are bind to the same dataset table as the
datagrid
and the text changes to reflect different row selected in the datagrid.

I want to save the changes that user make in the textboxes when they
select
a different row in the datagrid. I tried capturing the textbox.text at
datagrid's CurrentCellChanged event but by then the textbox.text is lost
already becuase the binding is to the new currently selected row.

private void VListing_CurrentCellChanged(object sender, System.EventArgs
e)
{
if(Changed)
{
dsVehicle.Tables["vehDetail"].Rows[prevRow]["Model"] = test;
prevRow = VListing.CurrentCell.RowNumber;
UpdateDataSet(dsVehicle);
}
Changed = false;
}

private void txtVModel_Validated(object sender, System.EventArgs e)
{
test = txtVModel.Text.ToString();

}
I tried capturing the textbox.text at textbox validated event and then
assign that to the dataset.tables.rows[col] and then update the database
but
that isn't working either. I did capter the textbox.text but the
assignment
to the dataset and update to the database is not working. Can someone
tell
me what's the best way to do this? Or point me to an example?



Thanks, Alpha
 

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