Click here to Skip to main content
15,033,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to update table by Datagridview Control
but i can not update table when I used VIEW CLASS
Could you help me?
How to update 2 table on datagridview?

1. On Form Load

  //CLASS view1 is join TB_Student and TB_Depart

Private Const SELECT_STRING As String = _
"SELECT * FROM view1"

  //The DataSet that holds the data

   Private m_DataSet As DataSet
   Dim data_adapter As SqlDataAdapter

  //Create the SqlDataAdapter.

   data_adapter = New SqlDataAdapter(SELECT_STRING, _

  //Map Table to Contacts.

   data_adapter.TableMappings.Add("Table", "Contacts")

  // Fill the DataSet.

   m_DataSet = New DataSet()

  //Bind the DataGrid control to the Contacts DataTable.


2. on Form1_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, _

       // Map Table to Contacts.
       data_adapter.TableMappings.Add("Table", "view1")

       // Make the CommandBuilder generate the
       // insert, update, and delete commands.

       command_builder = New _

       //Uncomment this code to see the INSERT,
       //UPDATE, and DELETE commands.
       //Debug.WriteLine("*** INSERT ***")
       //Debug.WriteLine("*** UPDATE ***")
       //Debug.WriteLine("*** DELETE ***")

       //Save the changes.

   End If

Updated 15-Oct-10 2:06am
Hiren solanki 29-Sep-10 4:38am
use 'pre' tags for code visibility.
Simon_Whale 29-Sep-10 6:06am
need to know what errors you are getting this would help us understand a problem

1 solution

Data modification when using Views is tricky.

See if this[^] (scroll down to the remarks section) helps you to get your head round it.

Somewhere on MSDN there is an article on updateable views in but I am unable to find it at the moment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900