我有一些文本框,我将数据插入表/ datagridview。我在datagridview中有一个搜索框和编辑功能。
当我没有使用以下代码将数据插入表格时,一切正常(编辑,搜索,删除等)如果我插入数据,则搜索和编辑不起作用。
下面有什么问题吗?
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
结束班