vb.net 输入后如何更新datagridview中的单元格?网络

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15413505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:47:57  来源:igfitidea点击:

How Update cell in datagridview after enter? vb.net

vb.netsql-server-2008data-bindingdatagridviewkeypress

提问by nettoon493

i want update value from datagridview to sql after enter on cell. but it don't work.

我想在输入单元格后将 datagridview 中的值更新为 sql。但它不起作用。

this code:

这段代码:

Private Sub dgvShow_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dgvShow.KeyPress

Dim sb3 As New StringBuilder
Dim da2 As New SqlDataAdapter

For i As Integer = 0 To dgvShow.Rows.Count - 2
            If dgvShow.Rows(i).Cells(0).Value IsNot Nothing Then
                sb3.Remove(0, sb3.Length())
                sb3.Append("UPDATE PositionLevelWelfare ")
                sb3.Append("SET wfDivision=@wfDivision,wfSection=@wfSection,wfPosition=@wfPosition,wfBaht=@wfBaht")
                sb3.Append(" FROM PositionLevelWelfare pw")
                sb3.Append(" WHERE pw.Run=@Run")
                Dim SqlEdit As String = ""
                SqlEdit = sb3.ToString()

                da2.SelectCommand.CommandText = SqlEdit
                da2.SelectCommand.Parameters.Add("@Run", SqlDbType.NVarChar).Value = dgvShow.Rows(i).Cells(0).Value
                da2.SelectCommand.Parameters.Add("@wfDivision", SqlDbType.NVarChar).Value = dgvShow.Rows(i).Cells(1).Value
                da2.SelectCommand.Parameters.Add("@wfSection", SqlDbType.NVarChar).Value = dgvShow.Rows(i).Cells(2).Value
                da2.SelectCommand.Parameters.Add("@wfPosition", SqlDbType.NVarChar).Value = dgvShow.Rows(i).Cells(3).Value
                da2.SelectCommand.Parameters.Add("@wfBaht", SqlDbType.NVarChar).Value = dgvShow.Rows(i).Cells(4).Value
                da2.SelectCommand.ExecuteNonQuery()
                da2.SelectCommand.Parameters.Clear() 
            End If
        Next
End Sub

Thanks for tour time. :)

感谢您的游览时间。:)

回答by Fabio

You can use Events DataGridView.CellBeginEditand DataGRidView.CellEndEditThen value updates after pressing Enter or when you leaving a current cell(click on other cell..)

按 Enter 键或离开当前单元格(单击其他单元格..)后,您可以使用 Events DataGridView.CellBeginEditand DataGRidView.CellEndEditThen 值更新

In the handler of DataGridView.CellBeginEdit

在处理程序中 DataGridView.CellBeginEdit

In this handler we just save a current(old) value of cell In this example I using a DataGridView.Tagproperty as place where to save a old value

在这个处理程序中,我们只保存单元格的当前(旧)值在这个例子中,我使用一个DataGridView.Tag属性作为保存旧值的地方

Private Sub dgv_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles dgv.CellBeginEdit
    If e.RowIndex < 0 OrElse e.ColumnIndex < 0 Then Exit Sub
    Me.dgv.Tag = Me.dgv.CurrentCell.Value
End Sub

Then in handler of DataGRidView.CellEndEdit

然后在处理程序中 DataGRidView.CellEndEdit

Private Sub dgv_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellEndEdit

    'Here you will get a new value of cell from dgv.CurrentCell.Value and compare with old value from dgv.Tag
    'You can add your checks for new value if you need. If some check fails just set
    dgv.CurrentCell.Value = dgv.Tag
    'If checks succeed then run your update to Database function with new value


End Sub

Or you can use dgv.CellValidatingevent, where you can use your check for new value and Cancelchanges if you need. And after this in CellEndEditjut run your UpdateTodatabase function Remember CellValidatingoccur before CellEndEdit

或者您可以使用dgv.CellValidating事件,您可以在其中使用检查以获取新值并Cancel根据需要进行更改。并在此之后CellEndEdit运行您的 UpdateTodatabase 函数记住CellValidating发生在之前CellEndEdit

MSDN DataGridView.CellValidating Event

MSDN DataGridView.CellValidating 事件