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

  • Home
  • SEARCH
  • 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 4589354
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:03:17+00:00 2026-05-21T22:03:17+00:00

Please could anyone point me in the right direction for the reentrant exceptions I’m

  • 0

Please could anyone point me in the right direction for the reentrant exceptions I’m getting when I try and change the datasource of a DataGridView control. Below is a cut down example of the problem that I am currently unable to find a suitable solution for. I come from a Visual Foxpro background where this sort of thing is easily possible.

The only way I’ve partially solved this is by running a thread to update the datasource but because this is asynchronous there is a small chance that the update will not happen. Another way that works is to have a separate button control to reorder the data but this involves the user pressing it – a much better way would be to automate it after the cell update.

I understand why the grid gets upset on changing a datasource when editing data of another source. I also know that the DataGridView can be set to sort on columns but I want to do more than just sort in my full version.

To run the code below add a DataGridView (naming it dgv1) to a form. Then to make the exception happen change the cell that has the 9 in to a 3 and then move off the cell by either clicking another cell or using the arrow keys. If enter is pressed before highlighting another cell then the exception doesn’t occur nor does the grid get reordered.

namespace dgv_test
{
public partial class Form1 : Form
{
    private List<dv> grid;
    public Form1()
    {
        InitializeComponent();
        // this would be from a database
        List<dv> data = new List<dv>
                        {
                            new dv{ desc="t1", order=1},
                            new dv{ desc="t2", order=2},
                            new dv{ desc="t3", order=9},
                            new dv{ desc="t4", order=4},
                            new dv{ desc="t5", order=5},
                        };
        // in memory list
        grid =
            (from lu in data
             orderby lu.order
             select new dv
             {
                 desc = lu.desc,
                 order = lu.order
             }).ToList();

        // grid list
        dgv1.DataSource =
            (from g in grid
             orderby g.order
             select new dv
             {
                 desc = g.desc,
                 order = g.order
             }).ToList();
        // make description column readonly
        dgv1.Columns[0].ReadOnly = true;
    }
    private void dgv1_CellLeave(object sender, DataGridViewCellEventArgs e)
    {
        // only update memory copy if order column changed
        if (dgv1.CurrentCellAddress.X == 1 && dgv1.IsCurrentCellDirty)
        {
            // grid is in memory copy of grid data
            grid.ElementAt(dgv1.CurrentCellAddress.Y).order = int.Parse(dgv1.CurrentCell.EditedFormattedValue.ToString());
            rgrid();
        }
    }
    // this is the function to update datagridview control
    private void rgrid()
    {
        // query the in memory list from the database
        var gx =
            (from g in grid
             orderby g.order
             select new dv
             {
                 desc = g.desc,
                 order = g.order
             }).ToList();
        // set the datagridview control with the newly ordered set
        dgv1.DataSource = gx.ToList();
        // do the same for the in memory list so that both are in alinement
        grid = gx.ToList();
    }
}
class dv
{
    public string desc { get; set; }
    public int order { get; set; }
}
}
  • 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-05-21T22:03:18+00:00Added an answer on May 21, 2026 at 10:03 pm

    Try moving your code to the CellValueChanged event of the DataGridView and it should work fine.

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

Sidebar

Related Questions

Could somebody please point me in the right direction for learning how to do
Could someone please demystify interfaces for me or point me to some good examples?
Ive decided that I really dont like microsoft and their ways. Please could you
Could somebody please do a rundown of how to programmatically encrypt a config-file in
Could somebody please name a few. I could given time, but this is for
Could you please show me the C# Equivalent of this VB.NET code: Public Partial
Could you please explain what the practical usage is for the internal keyword in
Could someone please tell me which objects types can be tested using Regular Expressions
Could someone please explain the best way to connect to an Interbase 7.1 database
Hi Guys could you please help me refactor this so that it is sensibly

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.