• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

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

0
Adnan
Asked:
Adnan
  • 11
  • 6
1 Solution
 
jorgesv13Commented:
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
 
jorgesv13Commented:
you should do a casting to bool before making the comparison

if ((bool)ReconManuallyGridView[0, i].Value != false)
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
AdnanAuthor Commented:
hmm oki, but know i get exeption saying that specified cast is not valid...?
0
 
jorgesv13Commented:
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
 
jorgesv13Commented:
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
 
jorgesv13Commented:
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
 
jorgesv13Commented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 11
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now