在网格顶部添加新行而不使用行指示符

时间:2014-10-24 16:17:23

标签: c# winforms gridview devexpress

我有一个按钮。单击它时,会在gridview中添加一个新行,并允许用户在该行中进行编辑。如何在第一行显示新行?

它看起来像是下一篇文章中最顶行但没有 https://documentation.devexpress.com/#windowsforms/CustomDocument778

目前,我确实喜欢以下代码。它有效但不好。使用此解决方案,我也遇到滚动条问题。如果网格有很多行,则会出现滚动条。添加新行时网格视图会稍微闪烁,因为

  • 它移动焦点并滚动到最后一行 - >最后一行被删除 - >顶部添加了新行 - >移动焦点并滚动到顶行

要禁用滚动,我按照文章https://www.devexpress.com/Support/Center/Question/Details/CQ19190创建了自定义网格视图。到现在为止还挺好。但问题是我不想自定义网格。有人有想法吗?

如果我需要自定义网格,如何在设计模式下将MainView转换为我的自定义网格? - 在设计模式下,单击网格视图的MainView,然后选择转换为...->自定义网格视图不会出现在菜单中

示例代码

private void AddNewRow(GridView gv) {
gv.AddNewRow();
var row = gv.GetRow(GridControl.NewItemRowHandle);

var helper = new ListDataControllerHelper(gv.DataController);

// Binding list has a method InserAt(index, object), but in my case the object can not be created directly.
// Hence, I do not know what it is. I only get new object through gv.AddNewRow()
// I can work around by extending binding list  
// but grid view does not support adding new row at specific position to adapt with InsertAt or my custom binding list 

// It is not a good idea
helper.BindingList.Remove(row); 
helper.BindingList.Insert(0, row); 
gv.FocusedRowHandle = 0;
gv.SetFocusedRowModified();
}

我将非常感谢所有的帮助。 谢谢

1 个答案:

答案 0 :(得分:0)

如果您只想添加一行而不自动更新布局,请尝试暂停和恢复布局,如下所示:

gv.SuspendLayout();
helper.BindingList.Remove(row); 
helper.BindingList.Insert(0, row); 
gv.FocusedRowHandle = 0;
gv.SetFocusedRowModified();
gv.ResumeLayout(true);

通常,插入行的方法似乎很好。