VB DataGridView中上移下移功能的实现

首先我用的这个办法必须数据库中有一个唯一列不是主键也不是标识列。我用代码让这一列自增,以便完成需要的功能。

数据库表如下


具体代码:

 Private Sub ToolStripButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton7.Click 这个是下一行
        Dim MySqlServer As New clsSqlServer("Server=PC-201108181735;Initial Catalog=CAMC_DCS;User ID=sa; Password=niu6351")
        Dim tet As String = Me.DataGridView1.Rows(Me.DataGridView1.CurrentCell.RowIndex).Cells("orderID").Value.ToString()
        Dim tet1 As String = Me.DataGridView1.Rows(Me.DataGridView1.CurrentCell.RowIndex).Cells("orderFrom_ID").Value.ToString() '上以前ID
        Dim bm As BindingManagerBase = Me.DataGridView1.BindingContext(Me.DataGridView1.DataSource) '使光标移动到下一行上
        bm.Position -= 1
        Dim txt As String = Me.DataGridView1.Rows(Me.DataGridView1.CurrentCell.RowIndex).Cells("orderID").Value.ToString() '
        Dim txt1 As String = Me.DataGridView1.Rows(Me.DataGridView1.CurrentCell.RowIndex).Cells("orderFrom_ID").Value.ToString() '上移后ID
        Dim sql As String = "update  OrderFrom set orderID='" & txt & "'where orderFrom_ID='" + tet1 + "'"
        Dim bool As Boolean = MySqlServer.Update(sql)
        Dim sqls As String = "update  OrderFrom set orderID='" & tet & "'where orderFrom_ID='" + txt1 + "'"
        Dim bools As Boolean = MySqlServer.Update(sqls)
        If bool = True And bools = True Then
            Dim sqss As String = "select * from OrderFrom order by orderID ASC" '对数据表进行重新排序
            Dim dss As DataSet = MySqlServer.GetDataSet(sqss)
            ds.Clear()
            Me.DataGridView1.DataSource = dss.Tables(0)
            Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(txt - 1).Cells(0)
        End If
    End Sub

 Private Sub ToolStripButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton7.Click 这个是上一行
        Dim MySqlServer As New clsSqlServer("Server=PC-201108181735;Initial Catalog=CAMC_DCS;User ID=sa; Password=niu6351")
        Dim tet As String = Me.DataGridView1.Rows(Me.DataGridView1.CurrentCell.RowIndex).Cells("orderID").Value.ToString()
        Dim tet1 As String = Me.DataGridView1.Rows(Me.DataGridView1.CurrentCell.RowIndex).Cells("orderFrom_ID").Value.ToString() '上以前ID
        Dim bm As BindingManagerBase = Me.DataGridView1.BindingContext(Me.DataGridView1.DataSource)
        bm.Position -= 1
        Dim txt As String = Me.DataGridView1.Rows(Me.DataGridView1.CurrentCell.RowIndex).Cells("orderID").Value.ToString()
        Dim txt1 As String = Me.DataGridView1.Rows(Me.DataGridView1.CurrentCell.RowIndex).Cells("orderFrom_ID").Value.ToString() '上移后ID
        Dim sql As String = "update  OrderFrom set orderID='" & txt & "'where orderFrom_ID='" + tet1 + "'"
        Dim bool As Boolean = MySqlServer.Update(sql)
        Dim sqls As String = "update  OrderFrom set orderID='" & tet & "'where orderFrom_ID='" + txt1 + "'"
        Dim bools As Boolean = MySqlServer.Update(sqls)
        If bool = True And bools = True Then
            Dim sqss As String = "select * from OrderFrom order by orderID ASC" '对数据表进行重新排序
            Dim dss As DataSet = MySqlServer.GetDataSet(sqss)
            ds.Clear()
            Me.DataGridView1.DataSource = dss.Tables(0)
            Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(txt - 1).Cells(0)
        End If
    End Sub

思路就是,每次移动的时候只把该行的唯一标识互相调换,然后再重新排序,就可以把其在DataGridView中显示的位置调换

你可能感兴趣的:(VB)