Move Previous / Next Record in DataGridView, C #

1

I am browsing the DataGridView records through the actions of the Anterior and Siguiente buttons, the current method that works correctly to scroll through each record. But I used this code for combining the same cells in DataGridView in Winforms .

Fictitious data for example:

  

Then with the method I have it moves in the registers one by one, but I need to move to the next one avoiding the record if it has the same value in the cell, like moving the previous record while it is not equal the value of the cell .

I have the following CurrentCellChanged :

int indice = 0; 

private void myDataGrid_CurrentCellChanged(object sender, EventArgs e)
{
     if (myDataGrid.CurrentRow != null)
        indice = myDataGrid.CurrentRow.Index;
}

Anterior button:

private void boton_anterior_Click(object sender, EventArgs e)
{
     int anterior = indice - 1;
     myDataGrid.CurrentCell = myDataGrid.Rows[anterior].Cells[myDataGrid.CurrentCell.ColumnIndex];
}

Botoon Siguiente :

private void boton_siguiente_Click(object sender, EventArgs e)
{
      int siguiente = indice + 1;
      myDataGrid.CurrentCell = myDataGrid.Rows[siguiente].Cells[myDataGrid.CurrentCell.ColumnIndex];
}
  

To solve the problem of moving to the previous / next record if the value of the cell does not repeat, I tried the following:

bool IsTheSameCellValue(int column, int row)
{
    DataGridViewCell cell1 = dataGridView1[column, row];
    DataGridViewCell cell2 = dataGridView1[column, row - 1];
    if (cell1.Value == null || cell2.Value == null)
    {
       return false;
    }
    return cell1.Value.ToString() == cell2.Value.ToString();
}

    private void boton_anterior_Click(object sender, EventArgs e)
    {
        int anterior = indice - 1;

        if (IsTheSameCellValue(0, anterior))
        {
            anterior--;
            myDataGrid.CurrentCell = myDataGrid.Rows[anterior].Cells[myDataGrid.CurrentCell.ColumnIndex];
        }
        else
            myDataGrid.CurrentCell = myDataGrid.Rows[anterior].Cells[myDataGrid.CurrentCell.ColumnIndex];
    }

    private void boton_siguiente_Click(object sender, EventArgs e)
    {
        int siguiente = indice + 1;

        if (IsTheSameCellValue(0, siguiente))
        {
            siguiente++;

            if (siguiente == myDataGrid.Rows.Count)
                siguiente--;

            myDataGrid.CurrentCell = myDataGrid.Rows[siguiente].Cells[myDataGrid.CurrentCell.ColumnIndex];
        }
        else
            myDataGrid.CurrentCell = myDataGrid.Rows[siguiente].Cells[myDataGrid.CurrentCell.ColumnIndex];
    }
  

How can I solve it?

Environment: Visual Studio 2010 - C # (WindowsForms) & .NET NetFramework 4

    
asked by J. Rodríguez 09.02.2018 в 14:48
source

2 answers

1

I solved it by invoking the event Click of the button within itself and adding / subtracting 1 according to the button. boton_anterior_Click(sender, e); , boton_siguiente_Click(sender, e); .

  

This way I will not move the row of DataGridView if the value is equal, only increase / decrease the index until finding a different value in the cell.

On the click of the Previous Button:

private void boton_anterior_Click(object sender, EventArgs e)
{
     int anterior = indice - 1;

     if (anterior <= 0)
     {
         indice = 0;
         anterior = 0;
         myDataGrid.CurrentCell = myDataGrid.Rows[anterior].Cells[myDataGrid.CurrentCell.ColumnIndex];
         return;
     }
     if (IsTheSameCellValue(0, anterior))
     {
         indice--;
         boton_anterior_Click(sender, e);
     }
     else
     {
         indice--;
         myDataGrid.CurrentCell = myDataGrid.Rows[anterior].Cells[myDataGrid.CurrentCell.ColumnIndex];
     }
 }

On the Next Button:

private void boton_siguiente_Click(object sender, EventArgs e)
{
    int siguiente = indice + 1;

    if (siguiente < myDataGrid.Rows.Count - 1)
    {
        if (IsTheSameCellValue(0, siguiente))
        {
            indice++;
            boton_siguiente_Click(sender, e);
        }
        else
           myDataGrid.CurrentCell = myDataGrid.Rows[siguiente].Cells[myDataGrid.CurrentCell.ColumnIndex];
}
  

If someone proposes a cleaner solution, I would appreciate it.

    
answered by 09.02.2018 / 20:21
source
0

If you are using DataSource to popularize the grid with a DTO you can use Lambda, LINQ or in the same store to save the number of repetitions of each grouped record (Ex: ID: 1 has 6 records in "Value" so that you would keep 6).

Something like the following:

 public class Opcion
{
    public int Id { get; set; }
    public string  Nombre { get; set; }
    public int Repeticiones { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        //Simula el DTO
        List<Opcion> lista = new List<Opcion>();

        //count 4
        lista.Add(new Opcion { Id = 1, Nombre = "option 1" });
        lista.Add(new Opcion { Id = 1, Nombre = "option 1" });
        lista.Add(new Opcion { Id = 1, Nombre = "option 1" });
        lista.Add(new Opcion { Id = 1, Nombre = "option 1" });

        //count 5
        lista.Add(new Opcion { Id = 2, Nombre = "option 2" });
        lista.Add(new Opcion { Id = 2, Nombre = "option 2" });
        lista.Add(new Opcion { Id = 2, Nombre = "option 2" });
        lista.Add(new Opcion { Id = 2, Nombre = "option 2" });
        lista.Add(new Opcion { Id = 2, Nombre = "option 2" });

        //Recorro el DTO y Seteo mi Lista de Id y Repeticiones para usar en el DataGridView
        List<Opcion> listaConRep = new List<Opcion>();

        int index = 0;

        //Utilizamos GroupBy Lambda para no recorrer toda la lista innecesariamente
        for (int i = 0; i < lista.GroupBy(x => new { x.Id }).Count(); i++)
        {
            //Usamos Count Lambda para setear las repeticiones
            lista[index].Repeticiones = lista.Count(x => x.Id == lista[index].Id);

            //Comprobacion con Console. 
            Console.WriteLine(lista[index].Repeticiones);

            //Seteamos nuestra lista de referencia de repeticiones
            listaConRep.Add(new Opcion { Id = lista[index].Id, Repeticiones = lista[index].Repeticiones });

            //Sumamos nuestro index
            index += lista[index].Repeticiones;
        }

        Console.ReadKey();
    }

}

Then, in the OnClick of Next or Previous, you can search the list by the Id (which you have in the 1st column of the grid) and use "Repetitions" to add or subtract.

    
answered by 09.02.2018 в 16:22