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

AdnanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jorge SanchezSoftware EngineerCommented:
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

0
AdnanAuthor Commented:
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'      

               
0
Jorge SanchezSoftware EngineerCommented:
you should do a casting to bool before making the comparison

if ((bool)ReconManuallyGridView[0, i].Value != false)
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

AdnanAuthor Commented:
hmm oki, but know i get exeption saying that specified cast is not valid...?
0
Jorge SanchezSoftware EngineerCommented:
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)
0
AdnanAuthor Commented:
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....?
0
AdnanAuthor Commented:
i mean dont stop on last selection..!! loop throow
0
Jorge SanchezSoftware EngineerCommented:
add a break instruction just after the line where you set the CurrentCell

 ReconManuallyGridView.CurrentCell = ReconManuallyGridView[3, i];
break;
0
AdnanAuthor Commented:
i set the break point as you sayd but know it has stop jumping......it stays on the same selection and not jumping throow....
0
Jorge SanchezSoftware EngineerCommented:
ah.. maybe it's staying in the first
try adding one to the initial iterator:

 for (int i = curr +1; .....
0
AdnanAuthor Commented:
without break point it jumps to last index and the 2 or 3 index....?
0
AdnanAuthor Commented:
and not
0
AdnanAuthor Commented:
oki but how can i correct this?
0
Jorge SanchezSoftware EngineerCommented:
Did you try adding one to the initial iterator?

Replacing:

for (int i = curr; i <= ReconManuallyGridView.Rows.Count - 1; i++) {

for

for (int i = curr+1; i <= ReconManuallyGridView.Rows.Count - 1; i++) {

Please also post your complete code
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AdnanAuthor Commented:
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;
                }


            }
        }
0
AdnanAuthor Commented:
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;
               }

           }

        }
0
AdnanAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.