2
votes

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
            }
        }
TrandRefresh();

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.

2

2 Answers

1
votes

Never Mind I figured it out.. The issue was that the Changed Column in the Datagrid did not have the dataproperty set to the Changed column in the table. (I also had to change the way I added the Changed column to the data table as the method shown above makes the field read only.)

-1
votes

you can use : first : select (focus) textbox out datagridview second : sendkeys.send("{F2}") to this textbox third : use your function to save the data inside datagridview BR Ashraf