Thomas

Thomas

  • NA
  • 111
  • 0

C# WinApp datagridview not saving changes

Apr 29 2013 4:34 PM


On a Winform App, I have a datagridview that allows the user to review a couple dates in a database. It displays the rows with the dates in a selected range then the user can change the dates on any of the rows and hit an update button to save those changes to the database. The form then refreshes the datagridview removing any rows that are no longer in the selected date range.

When the datagrid is first populated or refreshed, I pull the data from the database and and add a Changed column defaulted to False. That becomes the datasource for the Datagridview.

dadapt.Fill(lDT_transReview);

lDT_transReview.Columns.Add("Changed",typeof(Boolean), "false");

dgTransReview.DataSource = lDT_transReview;


To check if the date was actually changed (not just entering the same date), I store the original date in a temp variable on the begin edit event. Then on the end Edit event I compare the date and set a 'changed' boolean checkbox to true.

dgTransReview["Changed", e.RowIndex].Value = "True";


Then on the code to save the row, I'm using a foreach loop to see if the 'changed' column is true.

foreach (DataGridViewRow row in dgTransReview.Rows)

{

if (row.Cells["Changed"].Value.ToString() == "True")

{

//Code to save individual row with changes to database

}

}

TransRefresh();


The problem I'm getting is that the Changed cell does not appear to be keeping the value. when it gets to setting the value to True, the cells is Null before the update (but True after) and when I get to the foreach loop, I get an object not set error because the cell is null again.


Answers (3)