By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,218 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

How to filter data and then update it in datagridview?

P: 3
I wanted to filter the data, make changes and the save back to the database.
The SQL data adapter and SQL dataset are clashing with TableDataAdapter i.e., db1TableAdpater and DatabseDataSet i.e GreetingDataset.
I wanted to make them working for the above purpose.

I am also open for other methods of executing the code.

Please help!

Here's the code.

Expand|Select|Wrap|Line Numbers
  1. Imports System.Data
  2. Imports System.Console
  3. Imports System.Data.SqlClient
  4. Imports System.Data.OleDb
  5.  
  6. Public Class Form1
  7.  
  8.     Dim sqlcon As New SqlConnection
  9.     Dim ds As New DataSet
  10.     Dim dc As SqlCommand
  11.     Dim daa As New SqlDataAdapter
  12.     Dim bs As New BindingSource
  13.     Public Const ConnectionString As String = "Data Source=SQLORACLE;Initial Catalog=greeting;User ID=classic;password=stripes"
  14.  
  15.  
  16.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  17.         'TODO: This line of code loads data into the 'GreetingDataSet.db1' table. You can move, or remove it, as needed.
  18.         'Me.Db1TableAdapter.Fill(Me.GreetingDataSet.db1)
  19.  
  20.         sqlcon.ConnectionString = "Data Source=SQLORACLE;Initial Catalog=greeting;User ID=classic;password=stripes"
  21.         Try
  22.             sqlcon.Open()
  23.         Catch ex As Exception
  24.             MessageBox.Show(ex.Message, "Connection to H247 Failed", MessageBoxButtons.OK)
  25.         End Try
  26.         If sqlcon.State = 1 Then
  27.             Me.Text = "Connection successful"
  28.         End If
  29.         Call populate_combo(cboest, "ESTNO")
  30.         cboest.Sorted = True
  31.         cboest.Enabled = False
  32.         cbover.Enabled = False
  33.         Db1DataGridView.Visible = False
  34.         Db1BindingNavigator.Visible = False
  35.     End Sub
  36.  
  37.     Sub populate_Adapter_Dataset()
  38.         Dim daa As SqlDataAdapter
  39.         daa = New SqlClient.SqlDataAdapter("Select * from db1 where ESTNO = '" & cboest.Text & "' AND ESTVERSION = '" & cbover.Text & "' ", sqlcon)
  40.         daa.Fill(ds, "db1")
  41.  
  42.         Db1DataGridView.DataSource = ds.Tables("db1")
  43.         Me.Db1TableAdapter.Fill(Me.GreetingDataSet.db1)
  44.     End Sub
  45.  
  46.     Public Sub populate_combo(ByRef cmb1 As ComboBox, ByVal cbest As String)
  47.         dc = New SqlCommand("Select distinct " & cbest & " from db1", sqlcon)
  48.         Dim da As SqlClient.SqlDataReader = dc.ExecuteReader
  49.         cmb1.Items.Clear()
  50.         While da.Read
  51.             cmb1.Items.Add(da("" & cbest & ""))
  52.         End While
  53.         da.Close()
  54.     End Sub
  55.  
  56.     Public Sub populate_combo1(ByRef cmb2 As ComboBox, ByVal cbver As String)
  57.         dc = New SqlCommand("Select distinct estversion from db1 where estno = '" & cboest.Text & "' ", sqlcon)
  58.         Dim da As SqlClient.SqlDataReader = dc.ExecuteReader
  59.         cmb2.Items.Clear()
  60.         While da.Read
  61.             cmb2.Items.Add(da("" & cbver & ""))
  62.         End While
  63.         da.Close()
  64.     End Sub
  65.  
  66.     Private Sub Db1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Db1BindingNavigatorSaveItem.Click
  67.         Me.Validate()
  68.         Me.Db1BindingSource.EndEdit()
  69.         Me.Db1TableAdapter.Update(Me.GreetingDataSet.db1)
  70.  
  71.     End Sub
  72.  
  73.     Private Sub cmdselect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdselect.Click
  74.         cboest.Enabled = True
  75.         cmdselect.Enabled = False
  76.     End Sub
  77.  
  78.     Private Sub cboest_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboest.SelectedIndexChanged
  79.         cbover.Enabled = True
  80.         Call populate_combo1(cbover, "estversion")
  81.     End Sub
  82.  
  83.     Private Sub cbover_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbover.SelectedIndexChanged
  84.         Call populate_Adapter_Dataset()
  85.         cboest.Enabled = False
  86.         cbover.Enabled = False
  87.         cmdselect.Enabled = True
  88.         Db1DataGridView.Visible = True
  89.         Db1BindingNavigator.Visible = True
  90.     End Sub
  91. End Class
  92.  
Jan 19 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.