XtraGrd.GetRow(XtraGrd.FocusedRowHandle) 未获取焦点文本框的值



在我的WinForm应用程序中,当我想在xtragrid中添加一行时,我在获取焦点textbox的当前值时遇到问题。

假设我有一个绑定到Model.VchType.TitletextBox,在我单击保存按钮之前,我的焦点txtTitle上,我在上面输入了"title1"。

这是我的保存按钮事件代码:

Model.VchType row = xtraGrd.GetRow(xtraGrd.FocusedRowHandle) as Model.VchType;

在它到达这行代码中的断点后,我会null row.Title。并且仅在我单击保存按钮之前,焦点在txtTitle上时才会出现此问题。

--------更新------------

以下是一些模型代码:

[System.ComponentModel.DataAnnotations.Schema.Table("vwVchType", Schema = "Sle")]
[Serializable]
public class VchType : Entity
{
    private int _ID;
    [System.ComponentModel.DataAnnotations.Schema.Column]
    [RnDisplayName(typeof(Rnw.Sle.Properties.Resources), "ID")]
    public override int ID
    {
        get
        {
            return _ID;
        }
        set
        {
            _ID = value;
        }
    }
    private string _Title;
    [System.ComponentModel.DataAnnotations.Schema.Column]
    [RnDisplayName(typeof(Rnw.Sle.Properties.Resources), "Title")]        
    public string Title
    {
        get
        {
            return _Title;
        }
        set
        {
            _Title = value;
        }
    }
}

我还创建了设计师的专栏。

我填充了一个bindingSource,并在设计器中将网格datasource的属性设置为此绑定源。

而且我认为问题不在于列名,因为如果在我单击保存按钮之前我专注于另一个控制器,它工作正常并且我获得了row.Title值。

你需要调用

((GridView)xtraGrid.FocusedView).PostEditor();

gridView.PostEditor()这会将当前值保存到编辑器EditValue。然后,您需要调用 view.UpdateCurrentRow() 来验证焦点行并将其值保存到数据源。

所以你需要这样的东西

((GridView)xtraGrid.FocusedView).PostEditor();
((GridView)xtraGrid.FocusedView).UpdateCurrentRow();
Model.VchType row = xtraGrd.GetRow(xtraGrd.FocusedRowHandle) as Model.VchType;

您可以在保存数据之前聚焦另一个表单对象。所以打电话:

anyControl.Select();

在保存之前。这将关闭文本框中打开的编辑器,并将更改发布到数据源。通常,这应该由有时似乎缺乏的PostEditor();来完成。

最新更新