0

I have this line of code to put "TRUE" on all the first column of selectedrows:

For Each row As DataGridViewRow In DataGridView1.SelectedRows
     row.Cells(0).Value = True
Next

But it takes a lot of time to put a single value to this column if the count of rows are more than a hundred. row.Cells(0) is a boolean column, and I only need it to be set as checked/unchecked.

I have tried checking if there's a syntax that will set a value of a column, but I couldn't find anything useful from the syntax available and also from the search engine. Hope you could help me with this one. Thanks in advance

0

1 Answer 1

1

If the DataGridView is databound, you shouldn't directly modify the content of the cell. Instead, you should modify the databound object. You can access that object through the DataBoundItem of the DataGridViewRow e.g. c#:

MyObject obj = (MyObject)dataGridView.CurrentRow.DataBoundItem;
obj.MyProperty = newValue;

Note that the bound object should implement INotifyPropertyChanged so that the change is reflected in the DataGridView Found here

vb.net:

Dim obj As MyObject = DirectCast(dataGridView.CurrentRow.DataBoundItem, MyObject)
obj.MyProperty = newValue
3
  • im asking in vb.net... >.<.. why do you have to copy all of the original answer's contents? Apr 12, 2013 at 4:50
  • fair point I have included the vb.net for you. Was on my phone :S Apr 12, 2013 at 5:08
  • that's ok.. thanks.. but im referring to SELECTED ROWS.. not CURRENTROW.. should I use the same loop again but modifying only the values from bindingsource/databound object? Apr 12, 2013 at 5:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.