none
Iterations through a Dataset/datagrid

    Question

  • I am new to C# and dataset/datagrid processing.  When saving any changes, I need to know how to iterate through changed rows to evaluate column contents for validation.  The information I have at my disposal does not show any examples.  Any help would be greatly appreciated.

     

     

    Wednesday, September 13, 2006 11:26 PM

Answers

  • yes my fault, it should have been DataColumn! Ive modified the post now.

    how are you checking against DBNull? Tried this?

    foreach(DataRow currentRow in theModifiedRows.Rows)

    {

       foreach(DataColumn currentColumn in theModifiedRows.Columns)

       {

          if (currentRow[currentColumn] != DBNull.Value)

          {

             object currentColumnValue = currentRow[currentColumn]

          }

       }

    }

     

    Thursday, September 14, 2006 3:02 PM
    Moderator

All replies

  • you can get the modified rows of data in the dataset:

     

    DataTable theModifiedCollection = theDataSet.Tables[0].GetChanges(DataRowState.Modified);

     

    then you can go through the rows/columns of the "theModifiedCollection" and do your validation. Or you could validate the entire dataset, but this would be ineffecient as you may have x amount of rows of data, could be 10's or 100's or 1000's so doing it the way I have shown would be better as it would only get the rows of data that have been modified only.

     

    does this help?

    Thursday, September 14, 2006 2:27 AM
    Moderator
  • The part

    DataTable theModifiedCollection = theDataSet.Tables[0].GetChanges(DataRowState.Modified);

    I was aware of. But where I am stuck is once  I get the modified rows, I need to validate each one of them, I suppose through a loop.

    I need to know how to reference the row/column of each of the rows deteremined in the statement above.

     

    Thanks

    Thursday, September 14, 2006 11:38 AM
  • to go through each row:

     

    foreach(DataRow currentRow in theModifiedRows.Rows)

    {

       foreach(DataColumn currentColumn in theModifiedRows.Columns)

       {

          object currentColumnValue = currentRow[currentColumn]

       }

    }

     

    does this help?

    Thursday, September 14, 2006 1:00 PM
    Moderator
  • Almost there.  This works (except there was a typo.  it should have been foreach DataColumn in the second loop).  If currentColumnValue is null, I am trying to check against "" or Null and it does not equate.  In the debugger, it says the value is DBNull, but when I try to check against that, it give me a compiler error.

    Thanks for the info.  Sorry to be a pain, but this is really new to me.

    Thursday, September 14, 2006 2:50 PM
  • yes my fault, it should have been DataColumn! Ive modified the post now.

    how are you checking against DBNull? Tried this?

    foreach(DataRow currentRow in theModifiedRows.Rows)

    {

       foreach(DataColumn currentColumn in theModifiedRows.Columns)

       {

          if (currentRow[currentColumn] != DBNull.Value)

          {

             object currentColumnValue = currentRow[currentColumn]

          }

       }

    }

     

    Thursday, September 14, 2006 3:02 PM
    Moderator
  • Yes this works.  I was checking as

    if (currentRow[currentColumn] != DBNull)  I left off the .Value.

    Is there any way to, if this value is null, assign it another value, such as

     

    if (currentRow[currentColumn] == DBNull.Value)

    {

          currentRow[currentColumn] = "This Result";

    }

     

    This obviously does not work, but it is what I want to do.

     

    Thanks again for all your help.

     

     

    Thursday, September 14, 2006 3:23 PM
  • why can't you do:

    currentRow[currentColumn] = "new value";

     

    ?

    I've tried it and works fine and logically looks fine too. What errors are you getting?

    Thursday, September 14, 2006 3:33 PM
    Moderator
  • No error, it is just that it does not appear to be assigning the value.  The particular column is a NOT NULL column in the database, and I am trying to assign it a value, but after the iterations, the INSERT fails because it is saying the value is NULL.

     

     

    Thursday, September 14, 2006 3:53 PM
  • it will be assigning the value - when you do:

     

    currentRow[currentColumn] = "new value" //for example we assume that the current data type is of string, change appropriately to match the data type

     

    do a MessageBox.Show(currentRow[currentColumn].ToString()) after assigning the value, you will see that the value has been modified as you had stated.

    probably a problem with something else?

    Thursday, September 14, 2006 4:00 PM
    Moderator
  • Yep, the value in currentRow[currentColumn] is getting assigned, but still failing on the INSERT saying that the value is NULL.  Should I be assigning the value to the Bind variable somehow?  It is almost like the column is not getting set to the bind variable of :CLIENT_CD.

     

    The insert statement associated with this data adaptor is something like this

    insert into tablea (client_cd) values (:client_cd).  Client code is null and I am setting to a value (above), but when

    the dataadapter.update(table) is called, the INSERT statement fails because the value is null.

     

     

    Thanks

    Thursday, September 14, 2006 4:18 PM
  • I found the problem.  It was something I had hidden in the code.  Thanks so much for your patience and help.

     

     

     

     

    Thursday, September 14, 2006 4:20 PM
  • well the changes are being applied to the dataset itself, and thats what the dataadapter uses to send the values etc... to the database to update it/insert the new record.

    now, if its failing the INSERT statement then thats not the problem for your original problem. When you retrieve the values from the database, I had thought you were validating the values so whatever nulls there may be, you wish to replace them - but now from this I believe you want to validate also before inserting the new records?

    Are you able to post your Insert statement, the insert codes you have?

    Thursday, September 14, 2006 4:22 PM
    Moderator