Visual Basic 添加行函数在重置函数后不起作用
Visual Basic Add Row Function Not Working After Reset Function
我会澄清我的问题。这是背景:
在 Visual Studio 2019 年使用 Visual Basic 创建数据库
数据库由我必须考虑的设备和序列号等列表组成
我有搜索功能,过滤功能,还有一些编辑功能
我遇到的问题是,一旦我单击重置按钮(应该将 dataviewgrid 未保存的更改重置回上次保存的更改),添加行功能就不再有效。代码没有抛出错误,只是在单击按钮时拒绝添加新行(在点击重置按钮之前,添加行按钮工作正常)
下面是我如何使用 sql:
填充我的数据库
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim connection As New SqlConnection("Connection String Placeholder")
Dim Table As New DataTable()
Dim Adapter As New SqlDataAdapter("SELECT * FROM TrackMain$", connection)
Adapter.Fill(Table)
DataGridView1.DataSource = Table
bind_data()
nextId = Convert.ToInt32(dt.Rows(dt.Rows.Count - 1)(0)) + 1
End Sub
我还尝试对我的第一列进行数字排序,因此我在那里和我的添加行函数中也有 NextID 值(这可能是罪魁祸首)
这里是添加行然后重置函数:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim dr As DataRow = dt.NewRow()
dr(0) = nextId
dt.Rows.Add(dr)
nextId += 1
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim connection As New SqlConnection("Connection String Placeholder")
Dim Table As New DataTable()
Dim Adapter As New SqlDataAdapter("SELECT * FROM TrackMain$", connection)
Adapter.Fill(Table)
DataGridView1.DataSource = Table
nextId = Convert.ToInt32(dt.Rows(dt.Rows.Count - 1)(0)) + 1
End Sub
如有任何问题,请告诉我,谢谢
编辑:这是我的 bind_data() Sub
Private Sub bind_data()
connection.Open()
Dim cmdTxt As String = "SELECT * FROM TrackMain$"
da = New SqlDataAdapter(New SqlCommand(cmdTxt, connection))
Dim builder As SqlCommandBuilder = New SqlCommandBuilder(da)
da.Fill(dt)
Dim source As BindingSource = New BindingSource With {
.DataSource = dt
}
DataGridView1.DataSource = source
End Sub
这是我测试的全部代码,代码对我有用。
Private dt As DataTable = New DataTable
Private da As SqlDataAdapter
Private connection As SqlConnection = New SqlConnection("connection string")
Dim nextId As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
bind_data()
nextId = Convert.ToInt32(dt.Rows(dt.Rows.Count - 1)(0)) + 1
End Sub
' Update database by DataGridView.'
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = DataGridView1.Rows.Count - 1 To 0
Dim row As DataGridViewRow = DataGridView1.Rows(i)
If Not row.IsNewRow AndAlso row.Cells(0).Value Is Nothing Then
DataGridView1.Rows.RemoveAt(i)
End If
Next
DataGridView1.EndEdit()
da.Update(dt)
End Sub
' Add new Row.'
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim dr As DataRow = dt.NewRow()
dr(0) = nextId
dt.Rows.Add(nextId)
nextId += 1
End Sub
' Reset the dt and DataGridView.'
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim Table As New DataTable()
dt.Clear()
Dim Adapter As New SqlDataAdapter("SELECT * FROM TrueTrack", connection)
Adapter.Fill(dt)
DataGridView1.DataSource = dt
nextId = Convert.ToInt32(dt.Rows(dt.Rows.Count - 1)(0)) + 1
End Sub
Private Sub bind_data()
connection.Open()
Dim cmdTxt As String = "SELECT * FROM TrueTrack"
da = New SqlDataAdapter(New SqlCommand(cmdTxt, connection))
Dim builder As SqlCommandBuilder = New SqlCommandBuilder(da)
da.Fill(dt)
Dim source As BindingSource = New BindingSource With {
.DataSource = dt
}
DataGridView1.DataSource = source
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
connection.Close()
End Sub
编辑:
数据库设计(取消auto-Increment of 'Id'):
DataGridView按'Id'排序可参考以下代码。
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Me.DataGridView1.Sort(Me.DataGridView1.Columns("Id"), ListSortDirection.Ascending)
End Sub
然后更改代码以删除 DataGridView 行。
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If DataGridView1.Rows.Count > DataGridView1.CurrentCell.RowIndex + 1 Then
DataGridView1.Rows.RemoveAt(DataGridView1.CurrentCell.RowIndex)
End If
nextId = Convert.ToInt32(DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(0).Value) + 1
End Sub
我会澄清我的问题。这是背景:
在 Visual Studio 2019 年使用 Visual Basic 创建数据库 数据库由我必须考虑的设备和序列号等列表组成 我有搜索功能,过滤功能,还有一些编辑功能
我遇到的问题是,一旦我单击重置按钮(应该将 dataviewgrid 未保存的更改重置回上次保存的更改),添加行功能就不再有效。代码没有抛出错误,只是在单击按钮时拒绝添加新行(在点击重置按钮之前,添加行按钮工作正常)
下面是我如何使用 sql:
填充我的数据库Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim connection As New SqlConnection("Connection String Placeholder")
Dim Table As New DataTable()
Dim Adapter As New SqlDataAdapter("SELECT * FROM TrackMain$", connection)
Adapter.Fill(Table)
DataGridView1.DataSource = Table
bind_data()
nextId = Convert.ToInt32(dt.Rows(dt.Rows.Count - 1)(0)) + 1
End Sub
我还尝试对我的第一列进行数字排序,因此我在那里和我的添加行函数中也有 NextID 值(这可能是罪魁祸首)
这里是添加行然后重置函数:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim dr As DataRow = dt.NewRow()
dr(0) = nextId
dt.Rows.Add(dr)
nextId += 1
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim connection As New SqlConnection("Connection String Placeholder")
Dim Table As New DataTable()
Dim Adapter As New SqlDataAdapter("SELECT * FROM TrackMain$", connection)
Adapter.Fill(Table)
DataGridView1.DataSource = Table
nextId = Convert.ToInt32(dt.Rows(dt.Rows.Count - 1)(0)) + 1
End Sub
如有任何问题,请告诉我,谢谢
编辑:这是我的 bind_data() Sub
Private Sub bind_data()
connection.Open()
Dim cmdTxt As String = "SELECT * FROM TrackMain$"
da = New SqlDataAdapter(New SqlCommand(cmdTxt, connection))
Dim builder As SqlCommandBuilder = New SqlCommandBuilder(da)
da.Fill(dt)
Dim source As BindingSource = New BindingSource With {
.DataSource = dt
}
DataGridView1.DataSource = source
End Sub
这是我测试的全部代码,代码对我有用。
Private dt As DataTable = New DataTable
Private da As SqlDataAdapter
Private connection As SqlConnection = New SqlConnection("connection string")
Dim nextId As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
bind_data()
nextId = Convert.ToInt32(dt.Rows(dt.Rows.Count - 1)(0)) + 1
End Sub
' Update database by DataGridView.'
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = DataGridView1.Rows.Count - 1 To 0
Dim row As DataGridViewRow = DataGridView1.Rows(i)
If Not row.IsNewRow AndAlso row.Cells(0).Value Is Nothing Then
DataGridView1.Rows.RemoveAt(i)
End If
Next
DataGridView1.EndEdit()
da.Update(dt)
End Sub
' Add new Row.'
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim dr As DataRow = dt.NewRow()
dr(0) = nextId
dt.Rows.Add(nextId)
nextId += 1
End Sub
' Reset the dt and DataGridView.'
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim Table As New DataTable()
dt.Clear()
Dim Adapter As New SqlDataAdapter("SELECT * FROM TrueTrack", connection)
Adapter.Fill(dt)
DataGridView1.DataSource = dt
nextId = Convert.ToInt32(dt.Rows(dt.Rows.Count - 1)(0)) + 1
End Sub
Private Sub bind_data()
connection.Open()
Dim cmdTxt As String = "SELECT * FROM TrueTrack"
da = New SqlDataAdapter(New SqlCommand(cmdTxt, connection))
Dim builder As SqlCommandBuilder = New SqlCommandBuilder(da)
da.Fill(dt)
Dim source As BindingSource = New BindingSource With {
.DataSource = dt
}
DataGridView1.DataSource = source
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
connection.Close()
End Sub
编辑: 数据库设计(取消auto-Increment of 'Id'):
DataGridView按'Id'排序可参考以下代码。
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Me.DataGridView1.Sort(Me.DataGridView1.Columns("Id"), ListSortDirection.Ascending)
End Sub
然后更改代码以删除 DataGridView 行。
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If DataGridView1.Rows.Count > DataGridView1.CurrentCell.RowIndex + 1 Then
DataGridView1.Rows.RemoveAt(DataGridView1.CurrentCell.RowIndex)
End If
nextId = Convert.ToInt32(DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(0).Value) + 1
End Sub