How to jump a column in a datagrid to print the next one?

1

I have to print some documents, but when it comes to a document that does not exist it stops printing I need to be able to print it if it does not find it skip to the next field and continue printing the others

public class imprimirmanifiesto
        {
            public void imprimir(string dobleslash)
            {
                Process p = new Process();
                p.StartInfo = new ProcessStartInfo()
                {
                    FileName = "C:\Users\bodega\Documents\Manifiestos\" + dobleslash + ".pdf",
                    UseShellExecute = true,
                    Verb = "printto",
                    CreateNoWindow = true,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    //Arguments = printer,
                };
                p.Start();
            }
        }

Button

private void button2_Click_1(object sender, EventArgs e)
        {
            r = new imprimirmanifiesto();
            int pos = 0;
            bool a = true;
            try
            {
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (!row.Cells[1].Value.ToString().Contains("-1"))
                        r.imprimir(row.Cells[1].Value.ToString());
                }
            }
            catch (Exception q)
            {
                //MessageBox.Show("Verifique número de importación y seleccione un tipo");
            }
        }
    
asked by Saul Salazar 24.09.2018 в 22:18
source

1 answer

4

Do not use a try/catch , they should only be used for exceptions that can not be foreseen. In your case, it is very easy to modify your Imprimir method so that it checks if the file exists before doing anything and throwing an exception:

public class imprimirmanifiesto
{
    public void imprimir(string dobleslash)
    {
        Process p = new Process();
        string fichero= "C:\Users\bodega\Documents\Manifiestos\" + dobleslash + ".pdf";
        if (File.Exists(fichero)
        {
            p.StartInfo = new ProcessStartInfo()
            {
                FileName = fichero,
                UseShellExecute = true,
                Verb = "printto",
                CreateNoWindow = true,
                WindowStyle = ProcessWindowStyle.Hidden,
               //Arguments = printer,
            };
            p.Start();
        }
    }
}

As you see, before running the process, we use File.Exists to check if the file exists. If it does not exist, do not launch it, there is no exception and you will simply continue with the next one.

    
answered by 25.09.2018 / 13:51
source