Devexpress GridControl : Prevent new row added automatically

最后都变了- 提交于 2019-12-25 06:38:47

问题


Please guide me how to prevent new rows added automatically on DevExpress.XtraGrid.GridControl

I want to control when new rows is added, in my case i'm using a keydown event (CTRL + I ) for this task. But the grid keep adding new rows automatically if i move the focus (cursor pointer) to the area right below to the last row and click.

The GridControl.MainView is a BandedGridView, which contains the datasource.


回答1:


You can handle the ValidateRow event. If you set e.Valid = false you wont add a new row. So check if your object is empty or invalid and just if the needed values are typed you give the row free.

private void grvMyView_ValidateRow(object sender, ValidateRowEventArgs e)
{
            if (grvMyView.IsNewItemRow(e.RowHandle))
            {
               MyObject obj = grvMyView.GetRow(e.RowHandle) as MyObject;

               e.Valid = obj.IsValid();
            }
}



回答2:


You can use BandedGridView.OptionsView.NewItemRowPosition property. You can set its value to NewItemRowPosition.None to hide new item row.

Another way is to handle BandedGridView.ShownEditor event. Inside of this you can check if BandedGridView.FocusedRowHandle property is equals to GridControl.NewItemRowHandle and cancel editor activation.
Here is example:

private void bandedGridView1_ShowingEditor(object sender, CancelEventArgs e)
{
    if (bandedGridView1.FocusedRowHandle == GridControl.NewItemRowHandle)
    {
        // Do here additional checks if you need. After your checks set e.Cancel to true.
        e.Cancel = true;
    }
}



回答3:


As of version 15 you can simply set your TableView's NewItemRowPosition to NewItemRowPosition.None. Be sure to call CommitEditing() on your TableView first.



来源:https://stackoverflow.com/questions/24481980/devexpress-gridcontrol-prevent-new-row-added-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!