Use Data Grid to update DB


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Use Data Grid to update DB

  1. #1
    Join Date
    Apr 2004
    Posts
    1

    Use Data Grid to update DB

    I am trying to use a datagrid to pull info off of the sql server then preform updates when users have entered info. Right now my problem is sending the data back. I dont know how to go about doing this as I havent stumbled on a way to tell if the data is different and I am not comfortable with the databinding stuff to blindly try and update. I havent used DG's before so any help would be appreciated!

    Option Explicit On
    Imports System.Data.SqlClient

    Public Class Form1
    Inherits System.Windows.Forms.Form
    Public ds As New DataSet
    Public myCmd As SqlCommand
    Public myReader As SqlDataReader
    Public jobid(250) As String ' I fill jobid on load through a db query that I left out of the prog as it works

    #Region " Windows Form Designer generated code "

    Dim CallDataBindToDataGrid As New MethodInvoker(AddressOf Me.DataBindToDataGrid)
    Dim MyDataSet As DataSet
    Dim MyDataAdapter As SqlDataAdapter
    Dim MyConnection As New SqlConnection("Initial Catalog=Gress; Data Source=DB;Integrated Security=SSPI;")

    '* This sub is straight out of the code I found. It was supposed to work on a differnt
    ' * DB, so i dont know if the dataMember bit needs to be changed..
    Public Sub DataBindToDataGrid()
    DataGrid1.DataSource = MyDataSet
    DataGrid1.DataMember = "authors"
    MyDataAdapter = Nothing
    MyDataSet = Nothing
    End Sub

    Public Sub QueryDataBase(ByVal i As Integer)
    Dim MyQueryString As String = "SELECT * FROM tbl_JobPhase where jobid = " & i
    MyDataSet = New DataSet
    MyConnection.Open()
    Dim cmd As New SqlCommand(MyQueryString, MyConnection)
    MyDataAdapter = New SqlDataAdapter(cmd)
    MyDataAdapter.Fill(MyDataSet, "authors")
    MyConnection.Close()
    Me.BeginInvoke(CallDataBindToDataGrid)
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    QueryDataBase(jobid(ComboBox1.SelectedIndex))
    End Sub
    End Class

  2. #2
    Join Date
    Apr 2004
    Posts
    50
    I'm not sure if my answer is what you are asking for but anyway

    'first of all you need to create update, insert and delete SQL commands -- you can use nice VS.net editor (invoked when creating a new sqlDataAdapter from toolbox) or Command builder class (it will create the same as previous but could bbe used in code instead of design view) or you can write them your self

    private sub update_dg( byval table as data.dataTable)

    dim da as new data.sqlclient.sqldataadapter()

    dim insertC as new data.sqlclient.sqlcommand()
    'your code for sql command goes here

    dim updateC as new data.sqlclient.sqlcommand()
    'your code for sql command goes here

    dim deleteC as new data.sqlclient.sqlcommand()
    'your code for sql command goes here


    da.insertcommand = insertC
    da.updatecommand = updateC
    da.deletecommand = deleteC

    da.update(table) 'the rest is VB's work

    end sub

    I'm not sure about the correctness of all code but it should be near

    if this is not OK/not enough feel free to
    va_kubc_2@wiut.uz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center