Problem with DevExpress XtraGrid FocusedRowChanged event when changing data source

This problem bothered me for several years, and maybe someone here knows a simple solution, since I just ran into it again.

QUESTION: Is there a way to make XtraGrid โ€œforgetโ€ the current focused row index before a new (different) data source is assigned to the grid?

BACKGROUND We use XtraGrid as a kind of controller for what is displayed on another panel of multiprocessor Winform.

Now imagine a hypothetical scenario in which the XtraGrid data source continues to change according to the menu selection. Menu item 1 fills the grid with a list of today's dishes in the dining room: Id, Name. Menu item 2 fills the grid with a list of Clients that the user must call on that day: ID, Name. The important thing is that these are separate, separate data sources, and the grid data source is assigned and reassigned.

CRITICAL FACT ON THIS QUESTION: We want the FocusedRowChanged grid to be the only place where we delay the user's choice in the controller grid. We are a spaghetti-free shop. FocusedRowChanged is better than a click event because it also controls the keyboard. The line with focus contains the identifier of the detailed record that we need to extract from the database for display in panel No. 2. This works - most of the time.

: , , , . , ( ) - . , . Entrees, FocusedRowChanged , . . , , .

DevExpress - ( , ), GUID, FocusedRowChanged , GUID GUID , , . . .

, : XtraGrid "" , ?

+3
4

, GridView DoChangeFocusedRowInternal. . , , , . , GridView , .

public class MyGridView : GridView {
        protected override void DoChangeFocusedRowInternal(int newRowHandle, bool updateCurrentRow) {
            if(this.lockFocusedRowChange != 0) return;
            if(!IsValidRowHandle(newRowHandle))
                newRowHandle = DevExpress.Data.DataController.InvalidRow;
            if(FocusedRowHandle == newRowHandle) return; // <<<<<<
            int currentRowHandle = FocusedRowHandle;
            BeginLockFocusedRowChange();
            try {
                DoChangeFocusedRow(FocusedRowHandle, newRowHandle, updateCurrentRow);
            }
            finally {
                EndLockFocusedRowChange();
            }
            RaiseFocusedRowChanged(currentRowHandle, newRowHandle);
        }
    }

UPDATE

:

namespace MyXtraGrid {

        public class MyGridControl : GridControl {
            protected override BaseView CreateDefaultView() {
                return CreateView("MyGridView");
            }
            protected override void RegisterAvailableViewsCore(InfoCollection collection) {
                base.RegisterAvailableViewsCore(collection);
                collection.Add(new MyGridViewInfoRegistrator());
            }
        }

        public class MyGridViewInfoRegistrator : GridInfoRegistrator {
            public override string ViewName { get { return "MyGridView"; } }
            public override BaseView CreateView(GridControl grid) {
                return new MyGridView(grid as GridControl);
            }
        }
        public class MyGridView : GridView {
            public MyGridView(GridControl ownerGrid) : base(ownerGrid) { }
            public MyGridView() { }


            protected virtual bool RowEqual(int focusedRowHandle, int newRowHandle) {
                if(IsDesignMode)
                    return focusedRowHandle == newRowHandle;
                DataRow row1 = GetDataRow(focusedRowHandle);
                DataRow row2 = GetDataRow(newRowHandle);
                return row1 == row2;
            }

            protected override void DoChangeFocusedRowInternal(int newRowHandle, bool updateCurrentRow) {
                if(this.lockFocusedRowChange != 0) return;
                if(!IsValidRowHandle(newRowHandle))
                    newRowHandle = DevExpress.Data.DataController.InvalidRow;
                if(RowEqual(FocusedRowHandle, newRowHandle))
                    return;
                int currentRowHandle = FocusedRowHandle;
                BeginLockFocusedRowChange();
                try {
                    DoChangeFocusedRow(FocusedRowHandle, newRowHandle, updateCurrentRow);
                }
                finally {
                    EndLockFocusedRowChange();
                }
                RaiseFocusedRowChanged(currentRowHandle, newRowHandle);
            }
        }
    }
0

, , , . , gridview.FocusedRowHandle = -1 .

+3

FocusedRowObjectChanged

( DevExpress 16.1).

+1

You can subscribe to the DataSourceChanged event that will fire when the data source changes (you guessed it!) So that you can then use the GetFocusedObject () object and display the corresponding elements for another grid ...

0
source

All Articles