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. DataGrid control WinFroms

DataGrid control WinFroms

Scheduled Pinned Locked Moved Windows Forms
csharpwinformshelpannouncement
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.
  • S Offline
    S Offline
    somagunasekaran
    wrote on last edited by
    #1

    Hi, I am using Datagrid control in WinForms...So iam iam Using command buttons are Insert,Update,Delete,Cancel. so i need coding part for these commands Buttons.So pls help me. this Critical situation..Pls.. thanks Somasundaram

    J 1 Reply Last reply
    0
    • S somagunasekaran

      Hi, I am using Datagrid control in WinForms...So iam iam Using command buttons are Insert,Update,Delete,Cancel. so i need coding part for these commands Buttons.So pls help me. this Critical situation..Pls.. thanks Somasundaram

      J Offline
      J Offline
      John_Adams
      wrote on last edited by
      #2

      Hi, Below is the code snippet showing how we can insert, update and delete records using ADO.NET with DataGrid control. ------------------------------------------------------------------------- BEGIN CODE Private Const SELECT_STRING As String = _ "SELECT * FROM Contacts ORDER BY LastName, FirstName" Private Const CONNECT_STRING As String = _ "Data Source=Bender\NETSDK;Initial " & _ "Catalog=Contacts;User Id=sa" ' The DataSet that holds the data. Private m_DataSet As DataSet ' Load the data. Private Sub Form1_Load(ByVal sender As Object, ByVal e As _ System.EventArgs) Handles MyBase.Load Dim data_adapter As SqlDataAdapter ' Create the SqlDataAdapter. data_adapter = New SqlDataAdapter(SELECT_STRING, _ CONNECT_STRING) ' Map Table to Contacts. data_adapter.TableMappings.Add("Table", "Contacts") ' Fill the DataSet. m_DataSet = New DataSet() data_adapter.Fill(m_DataSet) ' Bind the DataGrid control to the Contacts DataTable. dgContacts.SetDataBinding(m_DataSet, "Contacts") End Sub Private Sub Form1_Closing(ByVal sender As Object, ByVal e _ As System.ComponentModel.CancelEventArgs) Handles _ MyBase.Closing If m_DataSet.HasChanges() Then Dim data_adapter As SqlDataAdapter Dim command_builder As SqlCommandBuilder ' Create the DataAdapter. data_adapter = New SqlDataAdapter(SELECT_STRING, _ CONNECT_STRING) ' Map Table to Contacts. data_adapter.TableMappings.Add("Table", "Contacts") ' Make the CommandBuilder generate the ' insert, update, and delete commands. command_builder = New SqlCommandBuilder(data_adapter) ' Uncomment this code to see the INSERT, ' UPDATE, and DELETE commands. 'Debug.WriteLine("*** INSERT ***") 'Debug.WriteLine(command_builder.GetInsertCommand.CommandText) 'Debug.WriteLine("*** UPDATE ***") 'Debug.WriteLine(command_builder.GetUpdateCommand.CommandText) 'Debug.WriteLine("*** DELETE ***") 'Debug.WriteLine(command_builder.GetDeleteCommand.CommandText) ' Save the changes. data_adapter.Update(m_DataSet) End If End Sub END CODE ------------------------------------------------------------------------- You may make some more enhancement in the code. For example, you may check the DataSet's HasChanges method to see if there are any changes been made before you go to create a new SqlD

      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