[英]Updating A DataGridView That Is Databound
I have a DataGridView
that is databound to a dataset. 我有一个
DataGridView
数据绑定到数据集。 However I am trying to run a Query no the datasource and update the DataGridView
to display the Query Results. 但是,我正在尝试运行无数据源查询并更新
DataGridView
以显示查询结果。
The code I'm using is: 我使用的代码是:
Public Sub Call_Filter()
Dim myCon = New OleDbConnection(My.Settings.Database_string)
Try
myCon.Open()
Catch ex As Exception
MsgBox("Sorry, An Error Occurred" & vbNewLine & _
"Database Not Found!" & vbNewLine & vbNewLine & _
"Error Message: " & ex.Message, MsgBoxStyle.OkOnly, "Could Not Locate Database")
Main.VIEW_SavingMessage.Visible = False
Exit Sub
End Try
Dim FilterAdaptor = New OleDbDataAdapter
Dim FilterAdaptorText = "SELECT ID, [Name Of Person], [SAP Job Number], [Site Name], [Asset Description], [Spares Supplier], [Supplier Contact Name], [Supplier Contact Phone Number], " & _
"[Supplier Contact Email], [Spares Description], [Part Number], [Quantity To Order], Cost, Comments, [Request Date], [Date Ordered], [Ordered By], [Invoice Received], " & _
"[Invoice Paid], [Method Of Payment], [Date Item Received], [Quote Attatchment], [Marked As Urgent] " & _
"FROM Spares " & _
"WHERE ([Name Of Person] = @NameOfPerson OR @NameOfPerson = '' OR @NameOfPerson IS NULL) AND " & _
"([SAP Job Number] = @SAPJobNo OR @SAPJobNo = '' OR @SAPJobNo IS NULL)"
Dim FilteredDataSet = New DataSet
Dim sqr As OleDbCommand = New OleDbCommand(FilterAdaptorText, myCon)
sqr.Parameters.Add("@NameOfPerson", OleDbType.VarChar).Value = Main.Name_Of_PersonToolStripTextBox.Text
If Main.FilterJobNo.Text <> "" Then
sqr.Parameters.Add("@SAPJobNo", OleDbType.Integer).Value = Main.FilterJobNo.Text
Else
sqr.Parameters.Add("@SAPJobNo", OleDbType.Integer).Value = DBNull.Value
End If
Try
sqr.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Sorry, An error occured!" & vbNewLine & _
"The database rejected the request" & vbNewLine & vbNewLine & _
"Error Message: " & ex.Message, MsgBoxStyle.OkOnly, "Database Refused Query")
Exit Sub
End Try
FilterAdaptor.SelectCommand = sqr
FilterAdaptor.Fill(FilteredDataSet)
Main.DataGridView1.DataSource = FilteredDataSet
myCon.Close()
End Sub
Whilst this code executes corretly, and throws no errors, it returns a blank row to the DataGridView
, not the correct filtered results. 尽管此代码可以正确执行,并且不会引发任何错误,但它会向
DataGridView
返回空白行,而不是正确的过滤结果。 I know the filter query works correctly as I have tested it through Access and it returns the correct inormation. 我知道筛选器查询可以正常工作,因为我已经通过Access测试了它,并且它返回了正确的信息。
I Can't use any other method that this as I have a large query to execute, this is just a fraction of the filter query for testing. 我不能使用任何其他方法来执行此操作,因为我要执行的查询很大,这只是用于测试的筛选查询的一小部分。
It appears that the issue I'm having is trying to get the filtered information in to my DataGridView
. 看来我遇到的问题是试图将过滤后的信息输入到
DataGridView
。 Am I missing something basic? 我缺少基本的东西吗? I've managed to insert, read and delete from databases in a similar method, but this doesn't seem to work when using a WHERE command.
我已经设法以类似的方法从数据库中插入,读取和删除数据库,但这在使用WHERE命令时似乎不起作用。
UPDATE - 29/06/15 更新-29/06/15
I'm still trying to get this to work, but after researching have changed my approach to this: 我仍在尝试使其工作,但经过研究后,我的处理方式发生了变化:
Public Function getDataSet() As DataSet
Dim ds As New Data.DataSet
ds.Tables.Add(New DataTable("Filtered"))
Dim Con As New OleDb.OleDbConnection(My.Settings.Database_string)
Con.Open()
Dim ad As New Data.OleDb.OleDbDataAdapter("SELECT ID, [Name Of Person] " & _
"FROM Spares " & _
"WHERE [Name Of Person] = Joe", Con)
ad.Fill(ds.Tables("Filtered"))
Return ds
End Function
Whats happening now is that I'm getting the following error: 现在发生的是我遇到以下错误:
No value given for one or more required parameters.
没有为一个或多个必需参数给出值。
But I don't understand where this supposed "Parameter" has come from? 但是我不明白这个所谓的“参数”是从哪里来的? I haven't used any parameters?
我没有使用任何参数?
Thanks 谢谢
There WHERE clause of your query is wrong. 您的查询的WHERE子句错误。
"WHERE ([Name Of Person] = @NameOfPerson OR @NameOfPerson = '' OR @NameOfPerson IS NULL) AND
Since the parameter/variable is just one condition for [Name of Person]
. 由于参数/变量只是
[Name of Person]
一种条件。 it should only appear once in the WHERE. 它只能在WHERE中出现一次。 If the name of the person is "Bob", that portion would resolve to:
如果此人的名字是“ Bob”,则该部分将解析为:
"WHERE ([Name Of Person] = 'Bob' OR 'Bob'= '' OR 'Bob' IS NULL) AND ...
Clearly, "Bob" is never going to be Null or ''
. 显然,“ Bob”永远不会为Null或
''
。 Those should all be the column name: 这些都应该是列名:
"WHERE ([Name Of Person] = 'Bob' OR [Name Of Person]= '' OR [Name Of Person] IS NULL) AND ...
The same would be true for the [SAP Job Number]
portion. [SAP Job Number]
部分也是如此。
You don't seem to be binding your data. 您似乎并没有绑定数据。 Try
DataGridView1.Databind()
after you set the Datasource. 设置数据源后,尝试使用
DataGridView1.Databind()
。
I finally managed to get it going using this: 我终于设法使用以下方法:
Public Function getDataSet() As DataSet
Dim ds As New Data.DataSet
ds.Tables.Add(New DataTable("Filtered"))
Dim Con As New OleDb.OleDbConnection(My.Settings.Database_string)
Con.Open()
Dim ad As New Data.OleDb.OleDbDataAdapter
Dim cmd As New OleDbCommand("SELECT * FROM Spares " & _
"WHERE ([Name Of Person] = @NOP OR [Name Of Person] = '' OR [Name Of Person] IS NULL Or @NOP IS NULL) AND " & _
"([SAP Job Number] = @SJN OR [SAP Job Number] = '' OR [SAP Job Number] IS NULL Or @SJN IS NULL)", Con)
If Main.Name_Of_PersonToolStripTextBox.Text = "" Then
cmd.Parameters.AddWithValue("@NOP", DBNull.Value)
Else
cmd.Parameters.AddWithValue("@NOP", Main.Name_Of_PersonToolStripTextBox.Text)
End If
If Main.FilterJobNo.Text = "" Then
cmd.Parameters.AddWithValue("@SJN", DBNull.Value)
Else
cmd.Parameters.AddWithValue("@SJN", Main.FilterJobNo.Text)
End If
ad.SelectCommand = cmd
ad.Fill(ds.Tables("Filtered"))
Return ds
End Function
and calling it using this: 并使用以下命令调用它:
Private Sub FILTER_Accept_Button_Click(sender As Object, e As EventArgs) Handles FILTER_Accept_Button.Click
DataGridView1.DataSource = Database_Management_Codes.getDataSet().Tables(0)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.