views:

35

answers:

1

Hey guys: I have a Datagridview, on wich i need to validate the user input on certains rows, and change it acording to it's value. As a example, if the user input a product code that doesn't exist on the DB, a search for a product dialog raises and search for the product and returns the correct code. So after reading around a lot, I decided to handle the CellFormating event: but I ran into two problems:

  1. The e.value is setted with the right value, but it doesn't persist
  2. And the event is raised like a millions of time, and sometimes ran into a SO exception.

And that's why i put a mbox in the event handler and now the programs does nothing but shows that mbox.

The point here is, even when the forms is newly created, the event raises a lot, and, everytime i move the mose pointer over any cell, the event is raised again.

What's the best option here? bellow is the code inside the event handler:

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        try
        {
            if (e.ColumnIndex == 0 && !dataGridView1.Rows[e.RowIndex].IsNewRow)
            {
                if (!Rep.Cajero_ProductoExiste(double.Parse(e.Value.ToString())))
                {
                    BuscarEIngresarProducto(ref e);
                }
            }
            MessageBox.Show("Event handler raised");
        }
        catch
        {
            e.FormattingApplied = false;
        }

    }

Where Rep.Cajero_ProductoExiste is a method that returns a bool=true if the product exists, and the method BuscarEIngresarProducto() is as below:

private void BuscarEIngresarProducto(ref  DataGridViewCellFormattingEventArgs e)
    {
        Busqueda b = new Busqueda(Rep, 2);
        if (b.ShowDialog() == DialogResult.OK)
        {
            e.Value = b.ProductoCodigo;
            dataGridView1.CurrentRow.Cells["pk"].Value = b.Producto;
            e.FormattingApplied = true;
        }
    }

Any help will be aprecciated. Thanks in advance (PD: sorry 4 my english, i'm spanish talker)

+1  A: 

You can try to handle CurrentCellDirtyStateChanged and check for IsCurrentCellDirty property. If it's true - validate input.

Jacob Seleznev
Thanks! There are way tooo many events in the datagridview, so i think i must start reading a lot of them.
josecortesp