按钮单击时向上或向下移动网格行

时间:2014-07-28 18:26:12

标签: asp.net vb.net

我有一个网格,我试图根据按钮点击向上或向下移动网格行。这是我到目前为止所拥有的。

 Protected Sub imgBtnMoveUp_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
    Dim imgBtn As ImageButton
    Dim FirstRow As GridViewRow = gvQuoteSo.Rows(0)
    Dim btnUp As Button = DirectCast(FirstRow.FindControl("MoveUp"), Button)
    Dim gvrow As GridViewRow
    Dim previousRow As GridViewRow
    Dim index As Integer = 0
    imgBtn = CType(sender, ImageButton)
    If imgBtn.CommandName = "MoveUp" Then
        index = Convert.ToInt32(imgBtn.CommandArgument)
        gvrow = gvQuoteSo.Rows(index)
        previousRow = gvQuoteSo.Rows(index - 1)
        UpdatePanelGrid.Update()
    End If
End Sub

当我点击按钮时,没有任何东西。我知道这只是向上移动功能,但如果我能得到帮助,那么向下移动将自行回答。非常感谢!

1 个答案:

答案 0 :(得分:0)

说明中给出了解释。

Private Sub btnUp_Click(sender As Object, e As EventArgs)
    Dim grid As DataGridView = Grid_view
    Try
        Dim totalRows As Integer = grid.Rows.Count ' will count the number of rows
        Dim idx As Integer = grid.SelectedCells(0).OwningRow.Index ' row index of the selected cell
        If idx = 0 Then ' for no rows selected
            Return
        End If
        Dim col As Integer = grid.SelectedCells(0).OwningColumn.Index ' columns corresponding to the selected row
        Dim rows As DataGridViewRowCollection = grid.Rows
        Dim row As DataGridViewRow = rows(idx)
        rows.Remove(row) ' Remove the corrent row
        rows.Insert(idx - 1, row)' Re-insert the row
        grid.ClearSelection()
            grid.Rows(idx - 1).Cells(col).Selected = True
    Catch
    End Try
End Sub

Private Sub btnDown_Click(sender As Object, e As EventArgs)

    Dim grid As DataGridView = Grid_view
    Try
        Dim totalRows As Integer = grid.Rows.Count
        Dim idx As Integer = grid.SelectedCells(0).OwningRow.Index
        If idx = totalRows - 2 Then
            Return
        End If
        Dim col As Integer = grid.SelectedCells(0).OwningColumn.Index
        Dim rows As DataGridViewRowCollection = grid.Rows
        Dim row As DataGridViewRow = rows(idx)
        rows.Remove(row)
        rows.Insert(idx + 1, row)
        grid.ClearSelection()
        grid.Rows(idx + 1).Cells(col).Selected = True
    Catch
    End Try
End Sub
相关问题