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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Binding DataGrid with Database

Binding DataGrid with Database

Scheduled Pinned Locked Moved C#
questioncssdatabasewpfwcf
2 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.
  • T Offline
    T Offline
    T i T i
    wrote on last edited by
    #1

    I have a question and I think it's so trivial: how can i update the database binded to the data grid when I change or edit any record or add a new record. I know how to retrieve data from the database and display it into the grid , but i need to update back the database with any update or addition to any record. Thank you so much.

    H 1 Reply Last reply
    0
    • T T i T i

      I have a question and I think it's so trivial: how can i update the database binded to the data grid when I change or edit any record or add a new record. I know how to retrieve data from the database and display it into the grid , but i need to update back the database with any update or addition to any record. Thank you so much.

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

      Read the DataAdapter.Update method documentation[^], which includes a lot of information about how to both fill a DataSet from a database to be bound to a DataGrid, as well as how to update the database from the changes in the DataSet using the Update method. The DataSet class - or rather the DataTable instances that it contains - can track changes to the tables. Do not call DataSet.AcceptChanges before calling DataAdapter.Update and the database is updated appropriately as long as you've assigned your UpdateCommand, InsertCommand, and DeleteCommand properties on the derivative DataAdapter (like SqlDataAdapter) correctly. It's also important to mention that for the InsertCommand you should also combine it with a SELECT statement (separated by semi-colons, as with most SQL expressions) so that auto-generated fields are updated to their appropriate values after inserting the records into the table in the database. The example referenced above also includes example source. For simple SELECT statements (assigned to the required SelectCommand, you can use the appropriate command builder (like the SqlCommandBuilder) to generate the other three statements. More complex statements require that you manually construct the expression. You can also get a lot of this for free from the designer. If you add a server reference to an RDBMS in the "Server Explorer", you can drag and drop a table, stored procedure, or view onto your Form or UserControl to create a connection and command object. You can then drag a DataAdapter derivative component from the "Data" toolbox category onto the designe and configure that programmatically. From that you can generate a typed DataSet (for faster look-ups and type safety). You can do those last two steps in reverse as well. Dragging and dropping the right components allows you to data-bind a DataGrid through the designer with minimal code. At the are minute, the following lines (or similar, depending on your field names) are r

      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