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. C#
  4. DataGrid and end edit

DataGrid and end edit

Scheduled Pinned Locked Moved C#
cssdatabasetutorialquestionannouncement
5 Posts 2 Posters 0 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.
  • C Offline
    C Offline
    cje
    wrote on last edited by
    #1

    I have a simple windows form with a DataGrid bound to a dataset from a database. When the form closes (event 'Closing') I want to flush all changes made in the grid to the database. Everything is fine except the current cell...if I have not left the cell, the data is not updated. So far I have tried private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e) { System.Windows.Forms.DataGridCell cell = this.dataGrid1.CurrentCell; DataGridColumnStyle dgc = this.dataGrid1.TableStyles[0].GridColumnStyles[cell.ColumnNumber]; bool good = this.dataGrid1.EndEdit(dgc, this.dataGrid1.CurrentRowIndex, false ); this.oleDbDataAdapter1.Update( this.dataSetFeature1 ); } without luck. Does any one know how to get the current state of the cell into the dataset during 'Closing'? thanks in advance cje

    H 1 Reply Last reply
    0
    • C cje

      I have a simple windows form with a DataGrid bound to a dataset from a database. When the form closes (event 'Closing') I want to flush all changes made in the grid to the database. Everything is fine except the current cell...if I have not left the cell, the data is not updated. So far I have tried private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e) { System.Windows.Forms.DataGridCell cell = this.dataGrid1.CurrentCell; DataGridColumnStyle dgc = this.dataGrid1.TableStyles[0].GridColumnStyles[cell.ColumnNumber]; bool good = this.dataGrid1.EndEdit(dgc, this.dataGrid1.CurrentRowIndex, false ); this.oleDbDataAdapter1.Update( this.dataSetFeature1 ); } without luck. Does any one know how to get the current state of the cell into the dataset during 'Closing'? thanks in advance cje

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      If you want help, be specific. Why is it not working? Are you geting an exception? What exception type are you getting? What message does it contain? Code similar to what you have is not only recommended in the .NET Framework SDK, but has worked for me and others in the past. More information would be helpful to help you solve your problem.

      Microsoft MVP, Visual C# My Articles

      C 1 Reply Last reply
      0
      • H Heath Stewart

        If you want help, be specific. Why is it not working? Are you geting an exception? What exception type are you getting? What message does it contain? Code similar to what you have is not only recommended in the .NET Framework SDK, but has worked for me and others in the past. More information would be helpful to help you solve your problem.

        Microsoft MVP, Visual C# My Articles

        C Offline
        C Offline
        cje
        wrote on last edited by
        #3

        Thanks for you interest. I would have been happier if an excpetion was thrown...there are no exceptions thrown, it just simply doesn't work. The text that was entered in to the current cell was not updated to the database. All other cells with modified text were updated correctly. If I modify the text, click on another cell and then close, the text updates properly it is only when I have not left the current cell that I have the problem. When you have had this work, was it in the 'Closing' event of the form? is there a better place to do this processing? thanks

        H 1 Reply Last reply
        0
        • C cje

          Thanks for you interest. I would have been happier if an excpetion was thrown...there are no exceptions thrown, it just simply doesn't work. The text that was entered in to the current cell was not updated to the database. All other cells with modified text were updated correctly. If I modify the text, click on another cell and then close, the text updates properly it is only when I have not left the current cell that I have the problem. When you have had this work, was it in the 'Closing' event of the form? is there a better place to do this processing? thanks

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Does DataGrid.CurrentCell return anything? Also, in DataGridColumnStyle.EndEdit, just use DataGridCell.RowNumber instead of DataGridCell.CurrentRowIndex. The number of internal instructions is a little less, giving you a small increase in performance. The Closing event is a good place to do this, although if you're doing this in the Form that contains your DataGrid, don't handle the Closing event; simply override OnClosing:

          protected override void OnClosing(CancelEventArgs e)
          {
          // Do your stuff, perhaps even setting e.Cancel if needs be
          base.OnClosing(e);
          }

          This results in much faster code, since the IL instruction callvirt is used instead of many calls to a MulticastDelegate, enumeration of the delegates (handlers), and invocation of the handlers. Overriding the event handler also gives you a little more control. While you should typically call the base class's method, there are some times you might not want to (like not calling base.WndProc for a particular method in order to surpress it from being handled at all). Note that with control events, if you don't call the method on the base class, the event will not be fired so any other handlers won't be invoked.

          Microsoft MVP, Visual C# My Articles

          C 1 Reply Last reply
          0
          • H Heath Stewart

            Does DataGrid.CurrentCell return anything? Also, in DataGridColumnStyle.EndEdit, just use DataGridCell.RowNumber instead of DataGridCell.CurrentRowIndex. The number of internal instructions is a little less, giving you a small increase in performance. The Closing event is a good place to do this, although if you're doing this in the Form that contains your DataGrid, don't handle the Closing event; simply override OnClosing:

            protected override void OnClosing(CancelEventArgs e)
            {
            // Do your stuff, perhaps even setting e.Cancel if needs be
            base.OnClosing(e);
            }

            This results in much faster code, since the IL instruction callvirt is used instead of many calls to a MulticastDelegate, enumeration of the delegates (handlers), and invocation of the handlers. Overriding the event handler also gives you a little more control. While you should typically call the base class's method, there are some times you might not want to (like not calling base.WndProc for a particular method in order to surpress it from being handled at all). Note that with control events, if you don't call the method on the base class, the event will not be fired so any other handlers won't be invoked.

            Microsoft MVP, Visual C# My Articles

            C Offline
            C Offline
            cje
            wrote on last edited by
            #5

            yes, DataGrid.CurrentCell has the proper values in it. I will try your other suggestions - thank you!

            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