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

Populate Unbound Data Grid View via Text Box values

100+
P: 294
I am trying to populate a data grid view based off of two text boxes, Start date and End date.

I am not sure how to work with DataGridViews or DataSets.
So if textbox1.text = x/x/2013 and textbox2.text = y/y/2014, then clicking a button will filter the records and display only those records with corresponding date values in the data grid view.

The biggest part I am struggling with is getting the records via search button. Here's what I have so far:

Expand|Select|Wrap|Line Numbers
  1.                 Dim SQLConnection As New SqlConnection("Data Source=SQLTEST_HR,4000\SQLEXPRESS;Integrated Security=True")
  2.         Dim SqlCommand As New SqlCommand
  3.         Dim SQLReader As SqlDataReader
  4.         Dim dt As New DataTable
  5.  
  6.         'Dim SqlCommand As New SqlCommand("Select * FROM PARTICIPANT WHERE FIRST_NM_TXT = " & Trim(SearchFirsttxt.Text) & "'", SQLConnection)
  7.  
  8.         SqlCommand.Connection = SQLConnection
  9.         SQLConnection.Open()
  10.         SqlCommand.CommandText = "Select * FROM PARTICIPANT WHERE FIRST_NM_TXT = '" & Trim(SearchFirsttxt.Text) & "' and LAST_NM_TXT = '" & SearchLastTxt.Text & "'"
  11.         Dim myAdapter As New SqlDataAdapter(SqlCommand)
  12.         myAdapter.Fill(dt)
  13.         DataGridView1.DataSource(dt)
  14.  
  15.         SQLConnection.Close()
Any ideas on what should follow?

Thanks
Jan 10 '14 #1
Share this Question
Share on Google+
1 Reply


100+
P: 294
I figured it out. Here is my solution for anyone wondering how to do the same thing.

Expand|Select|Wrap|Line Numbers
  1.         Dim SQLConnection As New SqlConnection("Your Connection String Here")
  2.         Dim SqlCommand As New SqlCommand
  3.         'Dim SQLReader As SqlDataReader
  4.         Dim dt As New DataTable
  5.  
  6.         SQLConnection.Open() 'Opens database Connection
  7.         SqlCommand.Connection = SQLConnection 'Assigns command to the connection
  8.         SqlCommand.CommandText = "Select * FROM PARTICIPANT WHERE FIRST_NM_TXT = '" & SearchFirsttxt.Text & "' and LAST_NM_TXT = '" & SearchLastTxt.Text & "';"
  9.         'SQL Command returns rows where values in database and textboxes are equal
  10.         Dim myAdapter As New SqlDataAdapter(SqlCommand) 'holds the data
  11.         myAdapter.Fill(dt) 'datatable that is populated into the holder (DataAdapter)
  12.         DataGridView1.DataSource = dt 'Assigns source of information to the gridview (DataTable)
  13.         SQLConnection.Close() 'Closes Connection for security measures
Jan 10 '14 #2

Post your reply

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