0
Answer

.NET datagridview single Event for Old and New RowIndex

Sarang Deshpande

Sarang Deshpande

9y
1.1k
1

 Hi

I am working on migrating VB application to C# windows application. VB application is using third party controls such as Farpoint
spread (Gridview in terms of .NET) that provides huge number of functionalities.

Since I am not using any third party control in my new application and I need to achieve this functionality in C#.NET manually by writing
my own code, I need your help to achieve below scenario.
 
Problem -
 
How to get previously clicked row index and current row index in same event of datagrid view ?

Objective 1 -
- If I am in 2nd row of datagridview and I click on a cell in 4th row, while leaving a cell in 2nd row, I need a single event for .NET datagridview, that will give me row index of previous row as well as row index of new row I am entering. (New RowIndex and Old RowIndex) and my scenario demands this information in SAME EVENT of a grid. How to make this possible ?

Objective 2 -
Each row is dynamic, First column has Drop down with Values "A" and "B" and second column is read only textbox cell.
If I select value "B", my second column is getting converted into dropdown with some values.
If I am changing my row, and If dropdown controls in two rows have same values "Value1", Gridview is merging the rows having same values .At the same time, my grid should detect new and old row index and refresh itself.

Al above scenarios I mentioned are already handled in VB third party control's spread called "Far Point spread".
Below is the event code snippet for readymade event -

Private Sub farpointSpreadGrid_LeaveCell(ByVal Col As Long, ByVal Row As Long, ByVal NewCol As Long, ByVal NewRow As Long, Cancel As Boolean)

Challenege  -
I am not able to retain NewRowIndex and OldRowIndex values.
 
 Since .NET does not provide common single event for getting previously clicked row index and current row index at same place, I tried to
achieve the same using below events -
Selectionchanged
CellLeave
CellValidating

 private void fpsStep4Results_CellLeave(object sender, DataGridViewCellEventArgs e)
  if (Newrow != oldRow)
             TEST_LeaveCell(oldRow, Newrow);

 public static int oldRow = 0;
        private void fpResults_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            oldRow = fpResults.CurrentCell.RowIndex;
        }

        public static int Newrow = 0;
        private void fpResults_SelectionChanged(object sender, EventArgs e)
        {
            Newrow = fpsStep4Results.CurrentCell.RowIndex;
              }

These events give me newRowIndex and OldRowIndex. BUT I need to refresh the grid when other row is clicked. So when grid is refreshed
these above events are getting called random number of times. Some times once, some time multiple times.
 
My OldRowIndex and NewRowIndex values are getting continuously reset due to below order of events getting fired -
Event 1       -    Selectionchanged
Event 2 - CellLeave
Event 3 - CellValidating

I dont have any control over these events, because when grid is refreshed, depending on controls present in each row
Some rows have only one dropdown, some rows have two dropdowns), these events get fired and mess up my logic to set NewRowIndex and
OldRowIndex.

Is there any solution on this problem ?

 
Please let me know if there is any single place to retrieve newRowIndex and OldRowIndex for Gridview when I leave a cell ?
 
(If this description too lengthy and confusing, let me know so that i can share separate document with screenshots).
I would really appreciate your help.
 
Regards
Sarang