0
votes

I have a DataGridView in which the user enters a value . As he enters , new row gets added and user is able to enter his new value.

enter image description here

As the user enters names , more rows keep getting added. When the user is done entering all names , he clicks a button and I need to do something using every name entered in the grid view.

 foreach (DataGridViewRow row in dataGridView1.Rows)
            {

                if (row.Cells[0].Value != null)
                {

                    string user = row.Cells[0].Value.ToString();
                   // do something using the string user
               }
           }

This is the code I am using to read each row. The problem I am facing is that v

row.Cells[0].Value doesn't seem to catch the last active row.

eg. if the user has just typed "Tim" , he is forced to go to the next empty row to ensure Tim also gets read by the above code. If he still keeps cursor in the Tim cell after typing , the above code doesn't capture "Tim" .

How can I solve this problem ? I would prefer to do this without using any CellChange event or any event. I have full row select as Selection Mode.

1

1 Answers

1
votes

Per this link, you need to call dataGridView1.EndEdit(). Note that this method will throw an exception if this edit can't be closed (i.e., the data in the cell does not satisfy all input validation criteria).