Populating a GridView with query based on textBox.text
Hi I have a form which populates a TextBox I want to use the textBox.text to then load a gridView with the results
something like
Private Sub TextBoxId_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxIdSearchExist.TextChanged
Dim cmd As New SqlCommand("select * from pay Where t_id='" + tId.Text + "'"
Try
conn.Open()
da.Fill(ds, "List")
Dim ds As DataSet = GetData(queryString)
If (ds.Tables.Count > 0) Then
AuthorsGridView.DataSource = ds
AuthorsGridView.DataBind()
Else
Message.Text = "Unable to connect to the database."
End If
End Sub
any ideas
thanks
M
mikejs
Junior Poster in Training
66 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Awesome thanks for the help I used
Private Sub RadTextBoxid_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadTextBoxId.TextChanged
GridView1.DataSource = bindingSource1
Dim connString As String = My.Settings.strConn
Dim conn As New SqlConnection(connString)
Dim ds As New DataSet
Dim strSQL As String = "SELECT * FROM payments WHERE t_id = '" & TextBoxId.Text & "'"
Dim da As New SqlDataAdapter(strSQL, conn)
Try
conn.Open()
Dim builder As New SqlCommandBuilder(da)
da.Fill(ds, "payments")
If ds.Tables.Count > 0 Then
bindingSource1.DataSource = ds.Tables(0)
Else
MessageBox.Show("No results found in database.")
End If
Catch ex As Exception
MessageBox.Show("Unable to connect to database, or an error occurred.")
End Try
conn.Close()
End Sub
I am using the text change event as the results are from a text box search
all working well thanks again
mikejs
Junior Poster in Training
66 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Is it possible to set the table headers at run time I tried to do it via gui but it just fills across after my table headers I already added ?
mikejs
Junior Poster in Training
66 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
I am trying to use
Private Sub InitializeDataGridViewSearchExist(ByVal ignored As Object, _
ByVal ignoredToo As EventArgs) Handles DataGridViewSearchExist.ColumnAdded
' Set the column header names.
DataGridViewSearchExist.Columns(0).Name = "Tenant ID"
DataGridViewSearchExist.Columns(1).Name = "First Name"
DataGridViewSearchExist.Columns(2).Name = "Last Name"
DataGridViewSearchExist.Columns(3).Name = "Paid"
DataGridViewSearchExist.Columns(4).Name = "Date"
End Sub
But I get error Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
I have 5 columns I am trying to change each column name?
mikejs
Junior Poster in Training
66 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
thanks worked a treat :-)
mikejs
Junior Poster in Training
66 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
For some reason this has stopped working the data does not appear if I remove the
DataGridViewSearchExist.AutoGenerateColumns = False
I can see the default headers and data ? I am using the following code to populate
Private bindingSource1 As New BindingSource
Private Sub RadTextBoxTenantIdSearchExist_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadTextBoxTenantIdSearchExist.TextChanged
'DataGridViewSearchExist.Columns(0).DefaultCellStyle.BackColor = Color.Aqua
'DataGridViewSearchExist.Rows(0).DefaultCellStyle.BackColor = Color.Aqua
DataGridViewSearchExist.DataSource = bindingSource1
Dim connString As String = My.Settings.strConn
Dim conn As New SqlConnection(connString)
Dim ds As New DataSet
Dim strSQL As String = "SELECT t_id, firstName, lastName, rentPaid, datePaid, propRef, rentDue FROM payments WHERE t_id = '" & RadTextBoxTenantIdSearchExist.Text & "'"
Dim da As New SqlDataAdapter(strSQL, conn)
Try
conn.Open()
Dim builder As New SqlCommandBuilder(da)
da.Fill(ds, "payments") 'Needs to be the name of the source table
If ds.Tables.Count > 0 Then
bindingSource1.DataSource = ds.Tables(0)
'RadTextBox1.Text = Total().ToString("c")
Else
RadDesktopAlert1.Show()
RadDesktopAlert1.CaptionText = "Sorry!"
RadDesktopAlert1.ContentText = "No results found in database"
End If
Catch ex As Exception
RadDesktopAlert1.Show()
RadDesktopAlert1.CaptionText = "Sorry!"
RadDesktopAlert1.ContentText = "Unable to connect to database, or an error occurred"
End Try
conn.Close()
da.Dispose()
End Sub
and this is my db
CREATE TABLE [dbo].[payments](
[id] [int] IDENTITY(1,1) NOT NULL,
[t_id] [varchar](10) NOT NULL,
[firstName] [varchar](20) NOT NULL,
[lastName] [varchar](20) NOT NULL,
[rentPaid] [decimal](18, 0) NOT NULL,
[datePaid] [date] NOT NULL,
[propRef] [varchar](10) NOT NULL,
[rentDue] [decimal](18, 0) NULL,
CONSTRAINT [PK_payments] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
it should work fine??
mikejs
Junior Poster in Training
66 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
mikejs
Junior Poster in Training
66 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Thanks for the reply I have followed all the steps above even created a new dataGridView but I get the same result I copied each field name to each column name moved .DataSource = bindingSource1 to the form onload
still blank results in the datagrid I have done this several times with the same result I am going to create a new form and see if that works
mikejs
Junior Poster in Training
66 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
If I could give you a hug I would thank you for your exceptional help it worked perfectly, sometimes I just hate computers any idea why it doesn't work via design mode and previous code or is just one of those things?
mikejs
Junior Poster in Training
66 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0