I’m currently trying to input information into a data grid view, but when I try to do it, if the user has got a cell selected that one won’t have a value afterwards, and i fixed this using a refreshedit(); at the end of the loop, however that meant that only the last row was written to at the end
here’s the code at the moment
foreach ( Contact c in currentBook.Contacts )
{
ContactsList.RowCount = i + 1;
ContactsList.Rows[ i ].Cells[ 1 ].Value = c.FirstName;
ContactsList.Rows[ i ].Cells[ 2 ].Value = c.Surname;
ContactsList.Rows[ i ].Cells[ 3 ].Value = c.Address;
ContactsList.Rows[ i ].Cells[ 4 ].Value = c.Town;
ContactsList.Rows[ i ].Cells[ 5 ].Value = c.County;
ContactsList.Rows[ i ].Cells[ 6 ].Value = c.Postcode;
ContactsList.Rows[ i ].Cells[ 7 ].Value = c.PhoneNum;
ContactsList.Rows[ i ].Cells[ 8 ].Value = c.Email;
i++;
}
this code gets an exception saying:
Operation did not succeed because the program cannot commit or quit a cell value change.
so i added in a
ContactsList.RefreshEdit();
after incrementing i however this means only the last row is displayed
I’ll be grateful for any help
thanks
Alex, you are using an unbound DataGridView as I can see from your code and I was able to reproduce the issue and solve it:
The DataGridView in this example has already three columns, added through the Visual Studio designer (right-click on the triangle of the DataGridView and subsequently add 3 columns to it as in the screenshot above shown). This is a prerequisite for the example below.
Take a look at this code (I’ve taken out some of the address fields to make it shorter and the DataGridView is named
dgvContactsListto distinguish it from the listcontactsList):Call this method in the following events:
Note that contactsList is in my example defined as follows (it is declared within the form class containing the DataGridView):
What is important in the example is that you create all the rows as needed before you change the values. Note that you can call the method
RefreshDgvContactmultiple times (e.g. from a refresh button) and it still works, because rows are only added if they don’t exist already.Also important to mention is that if you’re filling a DataGridView as shown here, it cannot be bound to a datasource at the same time. If you intended to bind the datasource, take a look at the other example already posted by David.
However, I think it is important to have both examples because sometimes it is useful to not bind a datasource, sometimes a bound datasource is more handy.
For those of you who want to reproduce the issue, you can replace the working
RefreshDgvContactsmethod by the following method (don’t forget to change the eventsForm1_Load()andbtnRefresh_Click()too):It is surprising to see that the position of the
dgvContactsList.Rows.Add()is important, isn’t it? With this code, the form loads as follows:Note that clicking the Refresh button works fine even with the method
RefreshDgvContacts_WithIssue().But now we’ve learned that we better use the first example, i.e.
RefreshDgvContacts()to load the DataGridView.