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?
please
Example:

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, _
       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,"view1")


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, _
           CONNECT_STRING)

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

       // 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


More http://www.vb-helper.com/howto_net_datagrid.htm[^]
Posted
Updated 15-Oct-10 2:06am
v3
Comments
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 vb.net/c# 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