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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:30:13+00:00 2026-05-13T23:30:13+00:00

This really seems like a bug to me, but perhaps some databinding gurus can

  • 0

This really seems like a bug to me, but perhaps some databinding gurus can enlighten me? (My WinForms databinding knowledge is quite limited.)

I have a ComboBox bound to a sorted DataView. When the properties of the items in the DataView change such that items are resorted, the SelectedItem in my ComboBox does not keep in-sync. It seems to point to someplace completely random. Is this a bug, or am I missing something in my databinding?

Here is a sample application that reproduces the problem. All you need is a Button and a ComboBox:

public partial class Form1 : Form
{
    private DataTable myData;

    public Form1()
    {
        this.InitializeComponent();

        this.myData = new DataTable();
        this.myData.Columns.Add("ID", typeof(int));
        this.myData.Columns.Add("Name", typeof(string));
        this.myData.Columns.Add("LastModified", typeof(DateTime));
        this.myData.Rows.Add(1, "first", DateTime.Now.AddMinutes(-2));
        this.myData.Rows.Add(2, "second", DateTime.Now.AddMinutes(-1));
        this.myData.Rows.Add(3, "third", DateTime.Now);

        this.myData.DefaultView.Sort = "LastModified DESC";
        this.comboBox1.DataSource = this.myData.DefaultView;
        this.comboBox1.ValueMember = "ID"; 
        this.comboBox1.DisplayMember = "Name";
    }

    private void saveStuffButton_Click(object sender, EventArgs e)
    {
        DataRowView preUpdateSelectedItem = (DataRowView)this.comboBox1.SelectedItem;
        // OUTPUT: SelectedIndex = 0; SelectedItem.Name = third
        Debug.WriteLine(string.Format("SelectedIndex = {0:N0}; SelectedItem.Name = {1}", this.comboBox1.SelectedIndex, preUpdateSelectedItem["Name"]));

        this.myData.Rows[0]["LastModified"] = DateTime.Now;

        DataRowView postUpdateSelectedItem = (DataRowView)this.comboBox1.SelectedItem;
        // OUTPUT: SelectedIndex = 2; SelectedItem.Name = second
        Debug.WriteLine(string.Format("SelectedIndex = {0:N0}; SelectedItem.Name = {1}", this.comboBox1.SelectedIndex, postUpdateSelectedItem["Name"]));

        // FAIL!
        Debug.Assert(object.ReferenceEquals(preUpdateSelectedItem, postUpdateSelectedItem));
    }
}

To clarify:

  • I understand how I would fix the simple application above–I only included that to demonstrate the problem. My concern is how to fix it when the updates to the underlying data rows could be happening anywhere (on another form, perhaps.)
  • I would really like to still receive updates, inserts, deletes, etc. to my data source. I have tried just binding to an array of DataRows severed from the DataTable, but this causes additional headaches.
  • 1 1 Answer
  • 3 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-13T23:30:13+00:00Added an answer on May 13, 2026 at 11:30 pm

    The only promising solution I see at this time is to bind the combo box to a detached data source and then update it every time the “real” DataView changes. Here is what I have so far. Seems to be working, but (1) it’s a total hack, and (2) it will not scale well at all.

    In form declaration:

    private DataView shadowView;
    

    In form initialization:

    this.comboBox1.DisplayMember = "Value";
    this.comboBox1.ValueMember = "Key";
    this.shadowView = new DataView(GlobalData.TheGlobalTable, null, "LastModified DESC", DataViewRowState.CurrentRows);
    this.shadowView.ListChanged += new ListChangedEventHandler(shadowView_ListChanged);
    this.ResetComboBoxDataSource(null);
    

    And then the hack:

    private void shadowView_ListChanged(object sender, ListChangedEventArgs e)
    {
        this.ResetComboBoxDataSource((int)this.comboBox1.SelectedValue);
    }
    
    private void ResetComboBoxDataSource(int? selectedId)
    {
        int selectedIndex = 0;
        var detached = new KeyValuePair<int, string>[this.shadowView.Count];
        for (int i = 0; i < this.shadowView.Count; i++)
        {
            int id = (int)this.shadowView[i]["ID"];
            detached[i] = new KeyValuePair<int, string>(id, (string)this.shadowView[i]["Name"]);
            if (id == selectedId)
            {
                selectedIndex = i;
            }
        }
        this.comboBox1.DataSource = detached;
        this.comboBox1.SelectedIndex = selectedIndex;
    }
    

    Must detach event handler in Dispose:

    this.shadowView.ListChanged -= new ListChangedEventHandler(shadowView_ListChanged);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, so this seems like a really silly problem but I can't get my
This seems like it should be really simple, but I'm unable to figure this
This seems like it should be really easy but I couln't find an answer
This really seems like a framework bug. The parameter is in the request's params
This really feels like a bug in Qt. Anyone has a solution or should
I have come across what seems like a really annoying bug running my C++
This question seems to get asked a lot, but I can't find an answer
This may seem like a realy basic question but... How do you use double
This seems really easy (I've done it a million times and never had a
This seems really strange to me: f = File.open(Dir.pwd+'/tmp','r') f.readlines do |l| puts #{f.lineno}:#{l}

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.