Dataset doesn't return true when dataset datarow has been modified

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I have a window C# application. I update the dataset when user finished
entering in a text box. But when I want to update the database when OK is
pressed, the if(dsVehicle.HasChanges(DataRowState.Modified)) would return
false and update of the dataset would take place. I do notice that if I
select other item that the textbox is bind to then the modified indicator
would then return true. Is there any code that I can add in the private void
txtVYear_Validated(object sender, System.EventArgs e) to indicate the dataset
row has been changed myself?

Thanks, Alpha

cmbScheudle.DataSource = dsSchedule.Tables["Schedule"];
cmbScheudle.DisplayMember = "ScheduleName";
cmbScheudle.ValueMember = "SchID";

//bind all the editable textboxes
cmbScheudle.DataBindings.Add("SelectedValue", dsVehicle.Tables["VehDetail"],
"ExtSchID");
txtVName.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"], "VName");
txtVIN.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"], "VIN");
txtVYear.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"], "Year");
txtVMaker.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"], "Make");
txtVModel.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"], "Model");
txtDMV.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"], "DMVLicense");
dtpDMVExp.DataBindings.Add("VALUE", dsVehicle.Tables["VehDetail"],
"DMVExpirationDate");
txtDOTReg.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"],
"DOTRegistration");
dtpDOTExp.DataBindings.Add("VALUE", dsVehicle.Tables["VehDetail"],
"DOTExpirationDate");
VListing.CurrentRowIndex = 0;


{
if ((!Loading) && (txtVYear.Text.Length>0))
dsVehicle.Tables["VehDetail"].Rows[VListing.CurrentRowIndex]["Year"] =
txtVYear.Text.ToString();
}

private void btnOK_Click(object sender, System.EventArgs e)
{
if(dsVehicle.HasChanges(DataRowState.Modified))
{
conVeh.Open();
sdaVehicle.Update(dsVehicle, "VehDetail");
conVeh.Close();
}
SaveMsg = false;
this.Close();
}
 
I think all you need to do is call CurrencyManager.EndCurrentEdit() prior to
doing the HasChanged check.

You'd basically do:

BindingContext[cmbScheudle.DataSource].EndCurrentEdit();

You may have to do it for the individual PropertyManager for the particular
TextBox that last had the focus. I'm not sure. I'm really just taking a stab
in the dark here. I haven't done much with binding controls other than grid
style controls.

I'm not positive your changes will get pushed to the dataset prior to the
EndCurrentEdit, though. Anyway, give these a try and if neither of these
work, I'll try to come up with another option.

Pete
 
Hi Pete, thank you for your reply. I noticed that it does save the
modification to the dataset if I click on another row on the datagrid which
the textbox is bind to. So I did the following code and it works. It's not
pretty but I tigger the datagrid selection row change event and it then
records the textbox changes to the dataset. Strange and not pretty but it
works for now. I would still like to find a better and more elegant way to
do this thougth.

private void txtVYear_Validated(object sender, System.EventArgs e)
{
if ((!Loading) && (txtVYear.Text.Length>0))
{
int curRow = VListing.CurrentRowIndex;
DataRow dr = dsVehicle.Tables["VehDetail"].Rows[VListing.CurrentRowIndex];
dr["Year"] = txtVYear.Text.ToString();
VListing.CurrentRowIndex = curRow;
}
}

Pete Davis said:
I think all you need to do is call CurrencyManager.EndCurrentEdit() prior to
doing the HasChanged check.

You'd basically do:

BindingContext[cmbScheudle.DataSource].EndCurrentEdit();

You may have to do it for the individual PropertyManager for the particular
TextBox that last had the focus. I'm not sure. I'm really just taking a stab
in the dark here. I haven't done much with binding controls other than grid
style controls.

I'm not positive your changes will get pushed to the dataset prior to the
EndCurrentEdit, though. Anyway, give these a try and if neither of these
work, I'll try to come up with another option.

Pete

Alpha said:
Hi, I have a window C# application. I update the dataset when user
finished
entering in a text box. But when I want to update the database when OK is
pressed, the if(dsVehicle.HasChanges(DataRowState.Modified)) would return
false and update of the dataset would take place. I do notice that if I
select other item that the textbox is bind to then the modified indicator
would then return true. Is there any code that I can add in the private
void
txtVYear_Validated(object sender, System.EventArgs e) to indicate the
dataset
row has been changed myself?

Thanks, Alpha

cmbScheudle.DataSource = dsSchedule.Tables["Schedule"];
cmbScheudle.DisplayMember = "ScheduleName";
cmbScheudle.ValueMember = "SchID";

//bind all the editable textboxes
cmbScheudle.DataBindings.Add("SelectedValue",
dsVehicle.Tables["VehDetail"],
"ExtSchID");
txtVName.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"], "VName");
txtVIN.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"], "VIN");
txtVYear.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"], "Year");
txtVMaker.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"], "Make");
txtVModel.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"],
"Model");
txtDMV.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"],
"DMVLicense");
dtpDMVExp.DataBindings.Add("VALUE", dsVehicle.Tables["VehDetail"],
"DMVExpirationDate");
txtDOTReg.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"],
"DOTRegistration");
dtpDOTExp.DataBindings.Add("VALUE", dsVehicle.Tables["VehDetail"],
"DOTExpirationDate");
VListing.CurrentRowIndex = 0;


{
if ((!Loading) && (txtVYear.Text.Length>0))
dsVehicle.Tables["VehDetail"].Rows[VListing.CurrentRowIndex]["Year"] =
txtVYear.Text.ToString();
}

private void btnOK_Click(object sender, System.EventArgs e)
{
if(dsVehicle.HasChanges(DataRowState.Modified))
{
conVeh.Open();
sdaVehicle.Update(dsVehicle, "VehDetail");
conVeh.Close();
}
SaveMsg = false;
this.Close();
}
 
I just ried using what you suggested in my other part of the code and it
worked great too. Thanks again.

Allpha

Pete Davis said:
I think all you need to do is call CurrencyManager.EndCurrentEdit() prior to
doing the HasChanged check.

You'd basically do:

BindingContext[cmbScheudle.DataSource].EndCurrentEdit();

You may have to do it for the individual PropertyManager for the particular
TextBox that last had the focus. I'm not sure. I'm really just taking a stab
in the dark here. I haven't done much with binding controls other than grid
style controls.

I'm not positive your changes will get pushed to the dataset prior to the
EndCurrentEdit, though. Anyway, give these a try and if neither of these
work, I'll try to come up with another option.

Pete

Alpha said:
Hi, I have a window C# application. I update the dataset when user
finished
entering in a text box. But when I want to update the database when OK is
pressed, the if(dsVehicle.HasChanges(DataRowState.Modified)) would return
false and update of the dataset would take place. I do notice that if I
select other item that the textbox is bind to then the modified indicator
would then return true. Is there any code that I can add in the private
void
txtVYear_Validated(object sender, System.EventArgs e) to indicate the
dataset
row has been changed myself?

Thanks, Alpha

cmbScheudle.DataSource = dsSchedule.Tables["Schedule"];
cmbScheudle.DisplayMember = "ScheduleName";
cmbScheudle.ValueMember = "SchID";

//bind all the editable textboxes
cmbScheudle.DataBindings.Add("SelectedValue",
dsVehicle.Tables["VehDetail"],
"ExtSchID");
txtVName.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"], "VName");
txtVIN.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"], "VIN");
txtVYear.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"], "Year");
txtVMaker.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"], "Make");
txtVModel.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"],
"Model");
txtDMV.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"],
"DMVLicense");
dtpDMVExp.DataBindings.Add("VALUE", dsVehicle.Tables["VehDetail"],
"DMVExpirationDate");
txtDOTReg.DataBindings.Add("Text", dsVehicle.Tables["VehDetail"],
"DOTRegistration");
dtpDOTExp.DataBindings.Add("VALUE", dsVehicle.Tables["VehDetail"],
"DOTExpirationDate");
VListing.CurrentRowIndex = 0;


{
if ((!Loading) && (txtVYear.Text.Length>0))
dsVehicle.Tables["VehDetail"].Rows[VListing.CurrentRowIndex]["Year"] =
txtVYear.Text.ToString();
}

private void btnOK_Click(object sender, System.EventArgs e)
{
if(dsVehicle.HasChanges(DataRowState.Modified))
{
conVeh.Open();
sdaVehicle.Update(dsVehicle, "VehDetail");
conVeh.Close();
}
SaveMsg = false;
this.Close();
}
 
Back
Top