Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. DataGridView not updating when DataSource updates

DataGridView not updating when DataSource updates

Scheduled Pinned Locked Moved Windows Forms
questioncsharpdatabasewpfwinforms
4 Posts 2 Posters 17 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mmfranke
    wrote on last edited by
    #1

    Hi. I'm new to ADO.NET, and clearly this question has been asked in different forms before, but nothing I'm reading is working. I'm writing a little sample Windows Forms app that is supposed to display the contents of a table in a database. I've got a simple "add row" button to add a new row, and I'd like to see my DataGridView show the changes when the row is added. The row gets added, but my DataGridView doesn't show it. Shouldn't I be able to register for an event that is fired when my database is updated (or is my DataSet?). I've tried calling EventsTableAdapter.Update, EventsBindingSource.ResetBindings, DataGridView.Refresh... all to no avail. Relevant source code to follow... public partial class Viewer : Form { private long m_NextEventID = 1; public Viewer() { InitializeComponent(); } private void Viewer_Load(object sender, EventArgs e) { this.eventsTableAdapter.Fill(this.dDBDataSet.Events); } // called on button click, to add a new row to my database. private void m_NewEasyEntryButton_Click(object sender, EventArgs e) { this.eventsTableAdapter.Insert( m_NextEventID++, DateTime.Now, DateTime.Now, false, 1, "Wrong Number", "This is a test"); // not working?!! this.eventsTableAdapter.Update(this.eventsTableAdapter.GetData()); this.eventsBindingSource.ResetBindings(false); this.m_DDBGridView.Refresh(); } } private void InitializeComponent() { ... this.eventsBindingSource = new System.Windows.Forms.BindingSource(this.components); this.dDBDataSet = new DiagViewer.DDBDataSet(); this.eventsTableAdapter = new DiagViewer.DDBDataSetTableAdapters.EventsTableAdapter(); this.m_NewEasyEntryButton = new System.Windows.Forms.Button(); this.m_UpdateNowButton = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.m_DDBGridView)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.eventsBindingSource)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.dDBDataSet)).BeginInit(); this.SuspendLayout(); // // m_DDBGridView // this.m_DDBGridView.DataBindings.Add(new System.Windows.Forms.Binding("Tag", this.eventsBindingSource, "ID", true)); this.m_DDBGridView.DataSource = this.eventsBindingSource; // // eventsBindingSource // this.eventsBindingSource.DataMember = "Events"; this.eventsBindingSource.DataSource = this.dDB

    R 1 Reply Last reply
    0
    • M mmfranke

      Hi. I'm new to ADO.NET, and clearly this question has been asked in different forms before, but nothing I'm reading is working. I'm writing a little sample Windows Forms app that is supposed to display the contents of a table in a database. I've got a simple "add row" button to add a new row, and I'd like to see my DataGridView show the changes when the row is added. The row gets added, but my DataGridView doesn't show it. Shouldn't I be able to register for an event that is fired when my database is updated (or is my DataSet?). I've tried calling EventsTableAdapter.Update, EventsBindingSource.ResetBindings, DataGridView.Refresh... all to no avail. Relevant source code to follow... public partial class Viewer : Form { private long m_NextEventID = 1; public Viewer() { InitializeComponent(); } private void Viewer_Load(object sender, EventArgs e) { this.eventsTableAdapter.Fill(this.dDBDataSet.Events); } // called on button click, to add a new row to my database. private void m_NewEasyEntryButton_Click(object sender, EventArgs e) { this.eventsTableAdapter.Insert( m_NextEventID++, DateTime.Now, DateTime.Now, false, 1, "Wrong Number", "This is a test"); // not working?!! this.eventsTableAdapter.Update(this.eventsTableAdapter.GetData()); this.eventsBindingSource.ResetBindings(false); this.m_DDBGridView.Refresh(); } } private void InitializeComponent() { ... this.eventsBindingSource = new System.Windows.Forms.BindingSource(this.components); this.dDBDataSet = new DiagViewer.DDBDataSet(); this.eventsTableAdapter = new DiagViewer.DDBDataSetTableAdapters.EventsTableAdapter(); this.m_NewEasyEntryButton = new System.Windows.Forms.Button(); this.m_UpdateNowButton = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.m_DDBGridView)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.eventsBindingSource)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.dDBDataSet)).BeginInit(); this.SuspendLayout(); // // m_DDBGridView // this.m_DDBGridView.DataBindings.Add(new System.Windows.Forms.Binding("Tag", this.eventsBindingSource, "ID", true)); this.m_DDBGridView.DataSource = this.eventsBindingSource; // // eventsBindingSource // this.eventsBindingSource.DataMember = "Events"; this.eventsBindingSource.DataSource = this.dDB

      R Offline
      R Offline
      RabidHamster
      wrote on last edited by
      #2

      The quickest solution is to call "this.eventsTableAdapter.Fill(this.dDBDataSet.Events)" after you insert the row. But this causes the entire grid to refresh, loosing the users scroll position and selection in the process. The problem is that the row gets added to the database, but is not added to the table adapter. The Fill method reloads the data from the database into the table adapter. The things you tried weren't working for various reasons. The Update method writes the changes that you just made to the database. It doesn't load changes back from the database. ResetBindings and Refresh didn't operate on the table adapter itself, which is what lacked the up-to-date data. There may be a more complex solution to force the table adapter to load just the inserted row, but I don't know of it. As an alternative to data binding, you might want to consider http://updatecontrols.net. These controls keep themselves up-to-date as your data changes. They don't bind directly to the database. Instead, they observe changes in your own data objects.

      M 2 Replies Last reply
      0
      • R RabidHamster

        The quickest solution is to call "this.eventsTableAdapter.Fill(this.dDBDataSet.Events)" after you insert the row. But this causes the entire grid to refresh, loosing the users scroll position and selection in the process. The problem is that the row gets added to the database, but is not added to the table adapter. The Fill method reloads the data from the database into the table adapter. The things you tried weren't working for various reasons. The Update method writes the changes that you just made to the database. It doesn't load changes back from the database. ResetBindings and Refresh didn't operate on the table adapter itself, which is what lacked the up-to-date data. There may be a more complex solution to force the table adapter to load just the inserted row, but I don't know of it. As an alternative to data binding, you might want to consider http://updatecontrols.net. These controls keep themselves up-to-date as your data changes. They don't bind directly to the database. Instead, they observe changes in your own data objects.

        M Offline
        M Offline
        mmfranke
        wrote on last edited by
        #3

        Dear RabidHamster, :-) Thanks very much for your timely, well-written, and complete response. I finally tried the Fill method, and deduced what was going on. I understood everything you said (and updatecontrols.net looks interesting!), but I feel I have to ask my question a little more deeply. Here's my bigger-picture problem. I want to use a database as a repository for diagnostics from my application (essentially like a log file -- on steroids). This will be updated by multiple processes, possibly distributed on a network, and I'd like to view it both "live," as the data comes in, and in an "analysis mode" where I have more sorting and filtering capability. I suppose I'll end up using a DataReader to do the "live" viewing, since it's lightweight and faster, and then I'll build a console window of sorts around that. I'll use a data-bound DataGridView of some sort to do the analysis features, which can be done on "snapshots" of the database. But even for the "snapshot" mode -- and especially for the "live" view, I want any UI to have the ability to know about changes to the database, since it's the central repository of information. Are you really saying there's no built in event mechanism that I can tap into that responds to changes to the database itself? Am I going to have to implement some sort of polling mechanism to check for changes -- and maybe fire those events myself? (and if so, what do I check for?) Thanks again.

        1 Reply Last reply
        0
        • R RabidHamster

          The quickest solution is to call "this.eventsTableAdapter.Fill(this.dDBDataSet.Events)" after you insert the row. But this causes the entire grid to refresh, loosing the users scroll position and selection in the process. The problem is that the row gets added to the database, but is not added to the table adapter. The Fill method reloads the data from the database into the table adapter. The things you tried weren't working for various reasons. The Update method writes the changes that you just made to the database. It doesn't load changes back from the database. ResetBindings and Refresh didn't operate on the table adapter itself, which is what lacked the up-to-date data. There may be a more complex solution to force the table adapter to load just the inserted row, but I don't know of it. As an alternative to data binding, you might want to consider http://updatecontrols.net. These controls keep themselves up-to-date as your data changes. They don't bind directly to the database. Instead, they observe changes in your own data objects.

          M Offline
          M Offline
          mmfranke
          wrote on last edited by
          #4

          Ummm... I think I might have found the answer to my problem: http://www.codeproject.com/cs/database/chatter.asp

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups