Question re:Adding Modifying and Deleting rows in a DataGridView
-
Monday, April 23, 2012 11:34 AM
I have a windows form with Master data - a recipe
and details data - Ingredients, Methods and Preperationthe details are managed in 3 dataGridViews there is a save button which when pressed goes through each of the Recipe, Ingredient, Method and Preperation Data saving changes in turn this allows the user to make a lot of changes for only one save.
This works OK until there are a lot of changes to one table and it appears to get confused when I make changes to a new row which has not got an ID to fetch it seems to change the status of the row from Added to modified.
It is Ok if I only make one or two changes but making a lot causes problems
jnc
All Replies
-
Monday, April 23, 2012 5:50 PMthis is the code for the management of the rows, there must be something missing somewhere or a miss-applied technique
foreach (DataRow changesRow in dtChanges.Rows)//loop this table
{ //save the changes UPDATE AND ADD NEW
DataRowState rsCh = changesRow.RowState;
//MODIFIED but will accept a modified new row
if (rsCh == DataRowState.Modified)
{
int chVal = 0;
if (DBNull.Value.Equals(changesRow["MethodID"]))
{ //if a new row is also modified it is flagged as modified but has no ID
isnew = true;
}
else
{
chVal = (int)changesRow["MethodID"];
}
string MO = "";
if (!DBNull.Value.Equals(changesRow["MethodOrder"]))
{
MO = (string)changesRow["MethodOrder"];
}
tblMethod tbM = db.tblMethods.SingleOrDefault(m => m.MethodID == chVal);
tbM.MethodOrder = MO;
string Met = "";
if (!DBNull.Value.Equals(changesRow["Method"]))
{
Met = (string)changesRow["Method"];
}tbM.Method = Met;
tbM.BulletType = (int)changesRow["BulletType"];
tbM.MethodType = (int)changesRow["MethodType"];//1 =prep or 2 = method
if (isnew)
{
db.tblMethods.InsertOnSubmit(tbM);
}
}
//ADDED
else if (rsCh == DataRowState.Added)
{
if (!DBNull.Value.Equals(changesRow["Method"]))
{//only save if the main field is not empty
string MO = "";
if (!DBNull.Value.Equals(changesRow["MethodOrder"]))
{
MO = (string)changesRow["MethodOrder"];
}tblMethod tbM = new tblMethod
{
RID = (int)changesRow["RID"],
MethodOrder = MO,
Method = (string)changesRow["Method"],
BulletType = (int)changesRow["BulletType"],
MethodType = (int)changesRow["MethodType"]//1 =prep or 2 = method
};db.tblMethods.InsertOnSubmit(tbM);
}
}
//DELETED
else if (rsCh == DataRowState.Deleted)
{
DataView cdv = new DataView(dtChanges, "", "", DataViewRowState.Deleted);foreach (DataRowView drv in cdv)
{
if (!DBNull.Value.Equals(drv[0]))
{
int chVal = (int)drv[0];
//if (chVal != 0)
{
tblMethod tbM = db.tblMethods.FirstOrDefault(m => m.MethodID == chVal);
db.tblMethods.DeleteOnSubmit(tbM);
}
}
else
{//not previously saved so has no ID
//tblMethod tbM = db.tblMethods.Single();//ch 23/04
tblMethod tbM = db.tblMethods.FirstOrDefault();
db.tblMethods.DeleteOnSubmit(tbM);
}
}
}
}jnc
-
Tuesday, April 24, 2012 1:39 PMModerator
Hi jnc,
>>if a new row is also modified it is flagged as modified but has no ID.
I'm not sure about it, if there are any changes on the added column, the state is still added. You can try my code here:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { new Program().DemonstrateRowState(); } private void DemonstrateRowState() { //Run a function to create a DataTable with one column. DataTable myTable = MakeTable(); DataRow myRow; // Create a new DataRow. myRow = myTable.NewRow(); // Detached row. Console.WriteLine("New Row " + myRow.RowState); myTable.Rows.Add(myRow); // New row. Console.WriteLine("AddRow " + myRow.RowState); // myTable.AcceptChanges(); // Unchanged row. // Console.WriteLine("AcceptChanges " + myRow.RowState); myRow["FirstName"] = "Scott"; // Modified row. Console.WriteLine("Modified, but the state is still " + myRow.RowState); myRow.Delete(); // Deleted row. Console.WriteLine("Deleted " + myRow.RowState); } private DataTable MakeTable() { // Make a simple table with one column. DataTable dt = new DataTable("myTable"); DataColumn dcFirstName = new DataColumn("FirstName", Type.GetType("System.String")); dt.Columns.Add(dcFirstName); return dt; } } }BTW, I'm not sure how do you define "isnew" field.
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

-
Tuesday, April 24, 2012 5:05 PM
Thanks Alan
As I loop through the changes table I seperate each row into Added, Modified or Deleted but some times if the table has a number of mixed changes like :
1.add a new row,
2.delete another
3.return to the added row and modifyThe added row's State is changed to Modified when it is really a NEW row which has been Modified before saving.
Its State should really be Added because when I try to save it is saving as a modifed row with a Zero ID the problem is that sometimes it works OKI hope I have made myself clear
jnc

