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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with Dataset 1

Status
Not open for further replies.

goneWildCoder

Programmer
May 26, 2004
20
US
I am having some problems figuring out the following:

1) Updating database with changes to dataset on Form Closing

Here is my form closing method:

private void DivFrmClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
int i;
if (dataSet11.HasChanges())
i = dbUpdate(); // method to update table

...
}

Row of data gets updated only when a new row has been added(focus is lost from that row)!!

How do I get around this to accomodate update of a row even when focus is not lost from curretn row?

2) I am trying to enforce an input string of length 2 characters for the 1st column. I set the MaxLength property to 2 and also do a check for strings of length less than 2. If it is so, I throw an Error Message . THe Error Message gets thrown twice ( The Message is in no kind of loop)....Here is what I have -->

private void Division_ColumnChanging(object sender, System.Data.DataColumnChangeEventArgs e)
{
//Check for errors in the Div_Code column
if (e.Column.ColumnName.Equals("Div_Code"))
{

if (e.ProposedValue.ToString().Length != 2)
{
MessageBox.Show("Invalid entry: Division Code must be 2 characters", "SPIRIT2 - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

// transfer control to cell
dataGrid1.CurrentCell = (DataGridCell) sender;

}

Please help!!

 
i'll start with number 2. this is because you change back to the cell containing the erroneous value (hence an extra change of collumns, therefore another call to your function). to ger around this, use a global variable (something like inProgramCall) and set it to true before changing columns:
Code:
private bool inProgramCall = false;
private void Division_ColumnChanging(object sender, System.Data.DataColumnChangeEventArgs e) 
{
  //Check for errors in the Div_Code column
  if (e.Column.ColumnName.Equals("Div_Code") && !inProgramCall) 
  {
    if (e.ProposedValue.ToString().Length != 2)
    {    
       MessageBox.Show("Invalid entry: Division Code must be 2 characters", "SPIRIT2 - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);            

      // transfer control to cell
      inProgramCall = true;                
      dataGrid1.CurrentCell = (DataGridCell) sender;
      inProgramCall = false;
    }
  }
}

1. first of all AFAIK HasChanges is a property not a method so you don't need the '()' after it. but since you have an event for changing columns, you might want to manually store the values inside the dataset (which will mark the row as changed therefore will be updated). add this to your ColumnChanging:
Code:
try
{
  dataSet11.Tables["table_name"].Rows[Division.CurrentCell.RowNumber][Division.CurrentCell.ColumnNumber] = Convert.ChangeType(Division.CurrentCell.ToString(), dataSet11.Tables["table_name"].Columns[Division.CurrentCell.ColumnNumber].DataType);
}
catch (Exception ex)
{
  MessageBox.Show(ex.Message);
}
just replace table_name with the name of the table that gets displayed in the datagrid.

let me know if it works for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top