newbie: updating table - proposed value

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

IDE: Visual Studio 2003 .NET
OS: XP pro

I have placed a datagrid on a form, and configured the datagrid to retrieve
data from the database...

I have also created a script that update the table, which the datagrid is
based on... The script update the table, but... If I enter values into a
cell and don't go out of the cell (let's say I don't press the tab-key)
before triggering the update script... the new value isn't saved to the
table... I think this have something to do with rowversion, and the new
value is just a proposed value

I have tried using the DataTable.AcceptChanges(), but it doesn't help..

This is my update script
try
{
SqlCommand cmd;
cmd = new SqlCommand("INSERT INTO BusinessLogic (bl_Code,
bl_Description) VALUES (@bl_Code, @bl_Description)",
qlCon);
cmd.Parameters.Add("@bl_Description", SqlDbType.VarChar, 50,
"bl_Description");
cmd.Parameters.Add("@bl_Code", SqlDbType.VarChar, 20, "bl_Code");
DataTable table = ds.Tables["BusinessLogic"];
table.AcceptChanges();
adapter.InsertCommand = cmd;
adapter.Update(table);
}
catch (SystemException er)
{
MessageBox.Show(er.Message);
}

Any clues to how I can save a proposed value will be appreciated!

Jeff
 
Hi Jeff,

You could programmatically change the currentcell back and forth before calling update, which will save the value.

DataGridCell c = dataGrid1.CurrentCell;
dataGrid1.CurrentCell = new DataGridCell(c.RowNumber+1, c.ColumnNumber);
dataGrid1.CurrentCell = c;

Note, this doesn't check if there is a valid rownumber above the current.
 
Hi Morten....

I've got a small problem with that solution... I have debugged it over and
over and still the new value isn't saved.. (remember I enter a value into a
datagridcell and then saving the data... without pressing the tab-key to
move out of the cell)...

Int32 nRow = dg_BusinessLogics.CurrentCell.RowNumber;
DataGridCell c = dg_BusinessLogics.CurrentCell;
dg_BusinessLogics.CurrentCell = new DataGridCell(c.RowNumber-1,
c.ColumnNumber);
dg_BusinessLogics.CurrentCell = c;
nRow = dg_BusinessLogics.CurrentCell.RowNumber;

In the script above, the nRow's value isn't changed

Here is the rest of my script:
SqlCommand cmd;
DataTable table = ds.Tables["BusinessLogic"];
table.AcceptChanges();

cmd = new SqlCommand("INSERT INTO BusinessLogic (bl_Code, bl_Description)
VALUES (@bl_Code, @bl_Description)", sqlCon);
SqlParameter paramDesc = new
SqlParameter("@bl_Description",SqlDbType.VarChar, 50);
paramDesc.SourceColumn = "bl_Description";
paramDesc.SourceVersion = DataRowVersion.Current;
SqlParameter paramCode = new SqlParameter("@bl_Code",SqlDbType.VarChar, 20);
paramCode.SourceColumn = "bl_Code";
paramCode.SourceVersion = DataRowVersion.Current;

cmd.Parameters.Add(paramDesc);
cmd.Parameters.Add(paramCode);
//sqlCon
adapter.InsertCommand = cmd;
adapter.Update(table);




Morten Wennevik said:
Hi Jeff,

You could programmatically change the currentcell back and forth before
calling update, which will save the value.
 
Well Jeff,

You call AcceptChanges before you call update. The effect is that the changes are saved in the dataset, but all rows gets reset to "Unchanged" so the Update method won't find any rows to update. Call AcceptChanges after Update.

In fact, you don't even need to move out of the cell for Update to save the values.
 
Back
Top