Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want use buttons as navigator to go throw the visible rows of a datagridview,I use this code to hide the rows :
private void Form1_Load(object sender, EventArgs e)

    {

        this.categoriaTableAdapter.Fill(this.mioOilMixDataSet.Categoria);
        for (int item = 0; item < categoriaDataGridView.Rows.Count - 1; item++)




        {
            if (categoriaDataGridView.Rows[item].Cells[2].Value.ToString().Contains("A"))
            {

                CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[categoriaDataGridView.DataSource];
                currencyManager1.SuspendBinding();
                categoriaDataGridView.Rows[item].Visible = true;
                currencyManager1.ResumeBinding();


            }
            else
            {
                CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[categoriaDataGridView.DataSource];
                currencyManager1.SuspendBinding();
                categoriaDataGridView.Rows[item].Visible = false;
                currencyManager1.ResumeBinding();
            }



        }

    }
C#



What I have tried:

and this to navigate :
private void button1_Click(object sender, EventArgs e)
    {

        foreach (DataGridViewRow row in categoriaDataGridView.Rows)
        {
            if (row.Visible)
            {
                categoriaBindingSource.MoveNext();
                break;
            }

        }

It's go to the next row,but show also the invisible rows,why ?
Posted
Updated 6-Oct-17 8:46am

1 solution

Hi,

This happened because the record is in the grid, even though you do not see. The method MoveNext don't have control about the visibility the row, it move for the next record in the grid, visible or not.

To do what you want. Do like this:
C#
private void button1_Click(object sender, EventArgs e)
{
   //find the current position selected
   int indexrow = categoriaBindingSource.Position;

   //read the rows until find the first visible row
   for (int item = indexrow; item < categoriaDataGridView.Rows.Count - 1; item++)
   {
        //verify if the next rows is visible
        if (categoriaDataGridView.Rows[(item + 1)].Visible)
        {
            //move the selector for this position
            categoriaBindingSource.Position = (item + 1);
            break;
        }
   }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900