datagrid保存时无法提交更新的问题

    技术2022-05-11  113

    如果绑定了datagrid,在datagrid中对数据进行更改后,点击窗体中的toolbar按钮进行保存时,最后输入的那个数据,如果焦点没有移开,在保存的时候就得不到提交,只有移开焦点的时候才能提交!

    在csdn的贴子上搜索了很久,发现很多人都是建议不用toolbar,而是用button控件,网友covis的解释是“用带SELECTED属性的控件提交数据即可。可能你用TOOLBAR的按钮或其它没有SELECTED属性的控件提交数据的。因为他不能被选中,所以他不能接受焦点,一般的按钮或控件可以将其它控件的焦点转移到自己身上。从这里去考虑。”原文在:http://search.csdn.net/expert/topic/52/5201/2003/4/21/1688615.htm

    但是由于笔者所有的toolbar都已经做好了,再更改起来麻烦也不美观,能不能找到其它方法来实现呢?

    经过一些摸索,发现了如下的方法可以实现:

        Public Sub SaveData()        '检查当前焦点是否在datagrid上,是就切换单元格,并提交更改。        If Me.ActiveControl.Parent.GetType Is GetType(DataGrid) Then            Dim dg As DataGrid = CType(Me.ActiveControl.Parent, DataGrid)            ChangeCurrentCell(dg)            Me.BindingContext(dg.DataSource).EndCurrentEdit()        End If

           .....save代码

        End Sub

        Public Function ChangeCurrentCell(ByVal dg As DataGrid) As Boolean        Try            Dim temp As DataGridCell = dg.CurrentCell

                dg.CurrentCell = New DataGridCell(temp.RowNumber, 0)            dg.CurrentCell = temp            Return True        Catch ex As Exception            Return False        End Try

        End Function


    最新回复(0)