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 242675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:51:27+00:00 2026-05-11T20:51:27+00:00

Situation: In my database I have a table in which records may reference other

  • 0

Situation:

In my database I have a table in which records may reference other records in the same table. Every record has a unique numeric ID (auto-ID column) and a nullable column IDRef which may contain the ID of the record to be referenced.

Now, in my C# Windows Forms application I’m using a DataGridView to allow the user to edit the records in the table. He may add, edit or remove items. I have a typed DataSet to hold the data and a corresponding TableAdapter to load the data from the database and perform updates. The DataGridView is bound to a BindingSource that uses the typed DataSet as its data source.

For the IDRef column I’ve added a ComboBox-column that populates its values from the same BindingSource as the DataGridView itself. That means the ComboBox is always up to date with the number of records.

Problem:

I’m displaying the ID column in the DataGridView. When adding new rows by clicking into the respective row of the DataGridView, the ID value is automatically populated with negative values. The final auto-ID value is computed only after persisting the changes to the database using the TableAdapter.

Now when I drop down the ComboBox for the IDRef column, I can select the previously existing records (for example 101, 102, etc.), but I can also select records that have been added after the last update to the database (for example -1, -2, -3).

Now: After I use the TableAdapter’s Update method to persist the changes to the database, these negative IDs do not exist anymore – I’m referencing non-existant records (in my case this leads to a foreign key constraint error).

Solution?

What I did is:

  • Use second DataSet and BindingSource for the ComboBox column
  • Add an “Apply” button that updates the changes to the database using the TableAdapter
  • Re-read the data into the second DataSet bound to the ComboBox column

This makes sure the user can only select values that have been persisted to the database by pressing the “Apply” button.

Is there a more elegant way than that to handle such “self-references” within tables using the DataGridView control?

EDIT: Changed tags to better fit subject

  • 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-11T20:51:27+00:00Added an answer on May 11, 2026 at 8:51 pm

    When using two different tables the negative-ID problem is solved by the relation, edit it’s properties and set it to be a Foreign key with Cascading updates.

    I would expect this to work for self-referencing as well but it’s tricky.

    On a separate note, I think you should have a different BindingSource (to the same table) for the combobox (or bind w/o a BindingSource).

    Note that you do have some other tools available as well, like Adapter.Update (dataRow)

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

Sidebar

Ask A Question

Stats

  • Questions 132k
  • Answers 132k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I alway prefer to use the NHibernate Event system to… May 12, 2026 at 6:30 am
  • Editorial Team
    Editorial Team added an answer They have to be managed objects, but you can convert… May 12, 2026 at 6:30 am
  • Editorial Team
    Editorial Team added an answer Yeah, a bit. Not enough to be a huge problem,… May 12, 2026 at 6:30 am

Related Questions

I have a situation in which a CSV is uploaded to my application and
I was experiencing a hard time creating FK relationships in my relational SQL database
I asked this question a while back but now I'm looking to implement an
The project I am working on requires some executions to be done at a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.