Link to home
Start Free TrialLog in
Avatar of Adnan
AdnanFlag for Norway

asked on

How to jump to next selection in a datagridview.....or jump between seleted rows

Hi

I have a datagridview with checkbox selection rows,every time i select cntrl-J i want to jump to next selection checkd in my datagridview.....
private void ReconManuallyGridView_KeyDown(object sender, KeyEventArgs e)
        {
 
            if (e.KeyData == (Keys.Control | Keys.Z))
            {
                UndoLastReconciled(this.myOper);
            }
            if (e.KeyData == (Keys.Control | Keys.J))
            {
                JumpToNextReconcildRow();
            }
        }
        private bool nextCell = false;
 
        private void JumpToNextReconcildRow()
        {
            if (!m_isChangingCell)
            {
                nextCell = true;
 
                this.ReconManuallyGridView.CurrentCell = ReconManuallyGridView[3, ReconManuallyGridView.CurrentRow.Index];
 
                nextCell = false;
            }
        }

Open in new window

Avatar of Jorge Sanchez
Jorge Sanchez
Flag of Ecuador image

First, you have to get the current index, and then loop in the next rows to see which one is checked

    int curr = ReconManuallyGridView.CurrentRow.Index;
    for (int i = curr; i <= ReconManuallyGridView.Rows.Count - 1; i++) {
        
        //change 0 for the index of your checkbox column
        if (ReconManuallyGridView[0, i].Value == true) {
            ReconManuallyGridView.CurrentCell = ReconManuallyGridView[3, i];
        }
    }
    

Open in new window

Avatar of Adnan

ASKER

Hi jorgesv13:


I get error on this  line if (ReconManuallyGridView[0, i].Value != false)
error: Error      6      Operator '!=' cannot be applied to operands of type 'object' and 'bool'      

               
you should do a casting to bool before making the comparison

if ((bool)ReconManuallyGridView[0, i].Value != false)
Avatar of Adnan

ASKER

hmm oki, but know i get exeption saying that specified cast is not valid...?
Are you sure yout first column is your checkbox column?
You should replace the "0" for the correct index.
Or maybe you can try specifying the name of the column.
Example:
if ((bool)ReconManuallyGridView["myCheckboxColumn", i].Value != false)
Avatar of Adnan

ASKER

hmm oki, its working know, but if i hvae more then one row checked i want to loop in selection and stop when it comes to last selection....?
Avatar of Adnan

ASKER

i mean dont stop on last selection..!! loop throow
add a break instruction just after the line where you set the CurrentCell

 ReconManuallyGridView.CurrentCell = ReconManuallyGridView[3, i];
break;
Avatar of Adnan

ASKER

i set the break point as you sayd but know it has stop jumping......it stays on the same selection and not jumping throow....
ah.. maybe it's staying in the first
try adding one to the initial iterator:

 for (int i = curr +1; .....
Avatar of Adnan

ASKER

without break point it jumps to last index and the 2 or 3 index....?
Avatar of Adnan

ASKER

and not
Avatar of Adnan

ASKER

oki but how can i correct this?
ASKER CERTIFIED SOLUTION
Avatar of Jorge Sanchez
Jorge Sanchez
Flag of Ecuador image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Adnan

ASKER

Hi  jorgesv13:

*yes i try adding one to intitial iterator it worked fine, but if selection is on last selection it wont jump to first one, how can i jump from last selection to first?


private void JumpToNextReconcildRow()
        {

            int curr = ReconManuallyGridView.CurrentRow.Index;
            //for (int i = curr; i <= ReconManuallyGridView.Rows.Count - 1; i++)
            for (int i = curr + 1; i <= ReconManuallyGridView.Rows.Count - 1; i++)
            {
                //change 0 for the index of your checkbox column
                if ((bool)ReconManuallyGridView["recon", i].Value != false)
                {
                    ReconManuallyGridView.CurrentCell = ReconManuallyGridView["recon", i];
                   
                    break;
                }


            }
        }
Avatar of Adnan

ASKER

I MADE IT.....      ;)

here is the code.....

 private void JumpToNextReconcildRow()
        {
            int counter;
            int curr = ReconManuallyGridView.CurrentRow.Index;
            for (int i = curr + 1; i <= ReconManuallyGridView.Rows.Count - 1; i++)
            {
               
                if ((bool)ReconManuallyGridView["Recon", i].Value == true)
                {
                    ReconManuallyGridView.CurrentCell = ReconManuallyGridView["Recon", i];
                    return;
                }
           }
           for (int i = 0; i <= curr; i++)
           {
               
               if ((bool)ReconManuallyGridView["Recon", i].Value == true)
               {
                   ReconManuallyGridView.CurrentCell = ReconManuallyGridView["Recon", i];

                   return;
               }

           }

        }
Avatar of Adnan

ASKER

thanks