Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8242187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:04:52+00:00 2026-06-07T21:04:52+00:00

I’m currently trying to input information into a data grid view, but when I

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:04:55+00:00Added an answer on June 7, 2026 at 9:04 pm

    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:

    DataGridView example

    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 dgvContactsList to distinguish it from the list contactsList):

        private void RefreshDgvContacts()
        {
            if (contactsList.Count > dgvContactsList.Rows.Count)
                dgvContactsList.Rows.Add(
                    contactsList.Count - dgvContactsList.Rows.Count
                );
            int i = 0;
            foreach (Contact c in contactsList)
            {
                dgvContactsList.Rows[i].Cells[0].Value = c.Firstname;
                dgvContactsList.Rows[i].Cells[1].Value = c.Surname;
                dgvContactsList.Rows[i].Cells[2].Value = c.Address;
                i++;
            }
        }
    

    Call this method in the following events:

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshDgvContacts();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            RefreshDgvContacts();
        }
    

    Note that contactsList is in my example defined as follows (it is declared within the form class containing the DataGridView):

        public List<Contact> contactsList = new List<Contact>()
        {   new Contact() {Firstname="Mark", Surname="Hamill", 
                           Address="Hollywood"},
            new Contact() {Firstname="Harrison", Surname="Ford", 
                           Address="Hollywood"}
        };
    
    public class Contact
    {
        [DataObjectField(false)]
        public string Firstname { get; set; }
    
        [DataObjectField(false)]
        public string Surname { get; set; }
    
        [DataObjectField(false)]
        public string Address { get; set; }
    }
    

    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 RefreshDgvContact multiple 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 RefreshDgvContacts method by the following method (don’t forget to change the events Form1_Load() and btnRefresh_Click() too):

        private void RefreshDgvContacts_WithIssue()
        {   // this sample leaves the 1st row blank called in Form1_Load
            // 2nd and subsequent calls, e.g. from the Refresh button don't show
            // the issue.
            int i = 0;
            foreach (Contact c in contactsList)
            {
                if (i >= dgvContactsList.Rows.Count)
                    dgvContactsList.Rows.Add();
                dgvContactsList.Rows[i].Cells[0].Value = c.Firstname;
                dgvContactsList.Rows[i].Cells[1].Value = c.Surname;
                dgvContactsList.Rows[i].Cells[2].Value = c.Address;
                i++;
            }
        }
    

    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:

    Example showing issue

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I want to construct a data frame in an Rcpp function, but when I
I have a view passing on information from a database: def serve_article(request, id): served_article

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.