Devexpress GridControl : Prevent new row added aut

2019-09-06 03:40发布

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.

enter image description here

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

3条回答
贪生不怕死
2楼-- · 2019-09-06 03:52

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;
    }
}
查看更多
Emotional °昔
3楼-- · 2019-09-06 03:56

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();
            }
}
查看更多
Deceive 欺骗
4楼-- · 2019-09-06 04:00

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

查看更多
登录 后发表回答