vb.net DataGridView未正确重新加载
我有一些文本框,可以将数据插入表/datagridview。我在datagridview中有一个搜索框和编辑功能 当我没有使用下面的代码将数据插入表中时,所有操作都有效(编辑、搜索、删除等)。如果我插入数据,则搜索和编辑不起作用 下面有什么问题吗vb.net DataGridView未正确重新加载,vb.net,datagridview,Vb.net,Datagridview,我有一些文本框,可以将数据插入表/datagridview。我在datagridview中有一个搜索框和编辑功能 当我没有使用下面的代码将数据插入表中时,所有操作都有效(编辑、搜索、删除等)。如果我插入数据,则搜索和编辑不起作用 下面有什么问题吗 Dim adap As New SqlDataAdapter("SELECT res_inv, res_snbr, First_Name, Last_Name, Item, Inventory_Start_Date, Inventory_End_Da
Dim adap As New SqlDataAdapter("SELECT res_inv, res_snbr, First_Name, Last_Name, Item, Inventory_Start_Date, Inventory_End_Date FROM Resident_InventoryLists2", cn)
Dim builder As New SqlCommandBuilder(adap)
Dim dt As New DataTable
Dim res_snbr As Object
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'CIInventoryDataSet.CurrentRoom_InventoryList' table. You can move, or remove it, as needed.
Me.CurrentRoom_InventoryListTableAdapter.Fill(Me.CIInventoryDataSet.CurrentRoom_InventoryList)
'TODO: This line of code loads data into the 'CIInventoryDataSet.Resident_InventoryLists2' table. You can move, or remove it, as needed.
Me.Resident_InventoryLists2TableAdapter.Fill(Me.CIInventoryDataSet.Resident_InventoryLists2)
DataGridView1.[ReadOnly] = False
'
adap.Fill(dt)
' ResidentInventoryViewBindingSource.DataSource = dt
ResidentInventoryLists2BindingSource.DataSource = dt
DataGridView1.DataSource = CurrentRoomInventoryListBindingSource
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.TextLength > 0 Then
CurrentRoomInventoryListBindingSource.Filter = String.Format("res_last_name Like '%{0}%'", TextBox1.Text)
' ResidentInventoryViewBindingSource.Filter = String.Format("res_last_name like '{0}*' Or res_snbr = '{0}'", TextBox1.Text)
Else
CurrentRoomInventoryListBindingSource.Filter = String.Empty
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
adap.Update(dt)
MessageBox.Show("Saved successfully")
Catch ex As Exception
MessageBox.Show("Error updating database")
End Try
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
Me.rnum.Text = DataGridView1.Item(0, i).Value
Me.rlastname.Text = DataGridView1.Item(1, i).Value
Me.rfname.Text = DataGridView1.Item(2, i).Value
Me.txtbed.Text = DataGridView1.Item(3, i).Value
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
cn.Open()
Dim cmd As New SqlCommand(("Insert INTO Resident_InventoryLists2 Values('" & _
rnum.Text & "','" & _
rfname.Text & "','" & _
rlastname.Text & "','" & _
txtitem.Text & "','" & _
StartDate.Text & "','" & _
EndDate.Text & "','" & _
txtCompany.Text & "')"), cn)
cmd.ExecuteNonQuery()
cn.Close()
rnum.Clear()
rfname.Clear()
rlastname.Clear()
rnum.Clear()
txtitem.Clear()
StartDate.Clear()
adap.Fill(dt)
Me.Resident_InventoryLists2TableAdapter.Fill(Me.CIInventoryDataSet.Resident_InventoryLists2)
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
If TextBox2.TextLength > 0 Then
' ResidentInventoryLists2BindingSource.Filter = String.Format("Last_Name Like '%{0}%'" Or "item like '%{0}%'", TextBox2.Text)
ResidentInventoryLists2BindingSource.Filter = String.Format("Last_Name like '{0}*'", TextBox2.Text)
Else
ResidentInventoryLists2BindingSource.Filter = String.Empty
End If
End Sub
Private Sub DataGridView2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellContentClick
For Each row As DataGridViewRow In DataGridView2.SelectedRows
DataGridView2.Rows.Remove(row)
Next
End Sub
最终类A)使用SQL参数;您可以使用StartDate更改数据类型,例如B)您可能应该在SQL中指定列名,以便每一位数据映射到正确的列。您可以举一个例子:您可能应该在SQL中指定列名,以便每一位数据映射到正确的列。你的意思是:rnum.Text&“MyColumnName”&\u我的SQL表即使是日期也全部是varchar
我的SQL表即使是日期也全部是文本
如果你想让日期像日期一样,不要这样做,因为所有的数据都插入到表中。这似乎不是问题所在。问题是一旦数据插入表中-我无法编辑/删除/搜索等)如果数据已经在表/数据网格中,我可以编辑/删除/搜索等。我认为这与以下两行代码有关:adap.Fill(dt)Me.Resident\u InventoryLists2TableAdapter.Fill(Me.CIInventoryDataSet.Resident\u InventoryLists2)