【问题标题】:Update checked Gridview rows更新选中的 Gridview 行
【发布时间】:2017-03-26 09:56:19
【问题描述】:

我在仅更新 GridView 中的选中行时遇到了问题,因为我只需要根据选中行的 ID 值更新一个列单元格值。

我试过这段代码,但它更新随机行,而不是检查:

        Try
            Dim I As Integer
            For I = 0 To GridView1.SelectedRowsCount() - 1
                If (GridView1.GetSelectedRows()(I) >= 0) Then
                    Dim strID As String = GridView1.GetRowCellValue(I, "ID")
                    Dim strUpdate As String = "UPDATE records SET payment_order= @payment_order where ID = @ID"
                    cmd.CommandType = CommandType.Text
                    cmd.CommandText = strUpdate.ToString()
                    cmd.Parameters.Clear()

                    cmd.Parameters.AddWithValue("@payment_order", rfnd_id_lbl.Text)
                    cmd.Parameters.AddWithValue("@ID", strID)
                    cmd.Connection = con
                    con.Open()
                    cmd.ExecuteNonQuery()
                End If
            Next


        Catch ex As SqlException
            Dim errorMsg As String = "Error in Update"
            errorMsg += ex.Message
            Throw New Exception(errorMsg)
        Finally
            con.Close()
        End Try

谁能帮我解决这个问题?

【问题讨论】:

    标签: vb.net xtragrid


    【解决方案1】:

    尝试使用gridView1.GetDataRow(gridView1.GetSelectedRows()[i]) 获取选定的记录,用于更新参数。

    希望对你有帮助。

    DX 文档:https://documentation.devexpress.com/WindowsForms/DevExpress.XtraGrid.Views.Base.ColumnView.GetSelectedRows.method

    【讨论】:

    • 您能否进一步详细说明为什么这种方法有助于解决 OP 的原始问题?将某人引导至文档不如解释解决方案为何起作用的过程有效。您确实简要说明了要使用什么,但需要更深入。
    • 我写的 ==> 使用 gridView1.GetDataRow(gridView1.GetSelectedRows()[i]) 代替 Dim strID As String = GridView1.GetRowCellValue(I, "ID")
    • 这无助于详细说明您的方法为何更有效或为什么会奏效。您可能会看到 OP 或我自己缺少的东西,但没有解释,您的答案只是一小段代码。 SO 的一部分解释了为什么,而不仅仅是如何。鉴于 OP 的问题是从 3 月开始的,您的答案可能会因含糊不清而被自动清理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多