Selection of chekbox in datagridview

2

Good afternoon. I have a datagridview that populates through a select database, and in that datagrideviwer I have a column with a chekbox, to select the line I want to send to the impression, only this is happening the next, when I select the line, and I send printing it is not obeying the selection and prints all lines.

Below, the code in which I populate the datagridview:

private void ListaGrid()
        {
            SqlCommand comando;

            StringBuilder Query = new StringBuilder();
            Query.Append(" SELECT                                                                            ");
            Query.Append("   CAST(0 AS bit) AS SELECIONAR                                                    ");
            Query.Append("   ,SC.C2_NUM AS GUIA                                                              ");
            Query.Append("   ,SB.B1_DESC AS PRODUTO                                                          ");
            Query.Append("   ,SC.C2_XNPAIS [PAÍS ORIGEM]                                                     ");
            Query.Append("   ,CONVERT(VARCHAR(10), CAST( SC.C2_XDTFAB AS DATE),103) AS [DT. FABRICAÇÃO]      ");
            Query.Append("   ,CONVERT(VARCHAR(10), CAST( SC.C2_XDTVALI AS DATE),103) AS [DT. VALIDADE]       ");
            Query.Append("   ,SC.C2_XLOTEF AS [LT. FABRICANTE]                                               ");
            Query.Append("   ,SC.C2_XLOTE AS [LT. INTERNO]                                                   ");
            Query.Append("   ,SB.B1_XDCB AS DCB                                                              ");
            Query.Append("   ,SB.B1_XCAS AS CAS                                                              ");
            Query.Append("   ,CONVERT(VARCHAR(10),CAST(SB.B1_PESO AS NUMERIC(15, 3))) + SB.B1_XSEGUM AS PESO ");
            Query.Append("   ,SC.C2_XNOMFA AS FABRICANTE ");
            Query.Append("   ,CB.CB0_CODETI AS [COD. BARRAS] ");
            Query.Append("   FROM SC2020 AS SC ");
            Query.Append("   INNER JOIN SB1020 AS SB WITH (NOLOCK) ON SB.B1_COD = SC.C2_PRODUTO ");
            Query.Append("   INNER JOIN CB0020 AS CB WITH (NOLOCK) ON CB.CB0_LOTE = SC.C2_XLOTE ");
            Query.Append("   WHERE C2_NUM = @C2_NUM ");

            comando = conex.CreateCommand();
            comando.CommandText = Query.ToString();

            comando.Parameters.Add("@C2_NUM", SqlDbType.VarChar).Value = txtGuiaFrac.Text.Trim();

            try
            {
                SqlDataAdapter dados = new SqlDataAdapter(comando);
                DataTable dtLista = new DataTable();
                dados.Fill(dtLista);

                DGW_EtqFracionamento.DataSource = dtLista;

                DGW_EtqFracionamento.Columns["GUIA"].ReadOnly = true;
                DGW_EtqFracionamento.Columns["PRODUTO"].ReadOnly = true;
                DGW_EtqFracionamento.Columns["PAÍS ORIGEM"].ReadOnly = true;
                DGW_EtqFracionamento.Columns["DT. FABRICAÇÃO"].ReadOnly = true;
                DGW_EtqFracionamento.Columns["DT. VALIDADE"].ReadOnly = true;
                DGW_EtqFracionamento.Columns["LT. FABRICANTE"].ReadOnly = true;
                DGW_EtqFracionamento.Columns["LT. INTERNO"].ReadOnly = true;
                DGW_EtqFracionamento.Columns["DCB"].ReadOnly = true;
                DGW_EtqFracionamento.Columns["CAS"].ReadOnly = true;
                DGW_EtqFracionamento.Columns["PESO"].ReadOnly = true;
                DGW_EtqFracionamento.Columns["FABRICANTE"].ReadOnly = true;
                DGW_EtqFracionamento.Columns["COD. BARRAS"].ReadOnly = true;
            }
            catch
            {
                MessageBox.Show("Não existem dados a serem encontrados");
            }
        }

Below, the code for the print button.

private void btnImprimir_Click(object sender, EventArgs e)
        {
            DataTable tableGrid = (DataTable)DGW_EtqFracionamento.DataSource;
            DataTable tableRelatorio = null;

            var filter = from DataRow row in tableGrid.Rows
                         where Convert.ToBoolean(row["SELECIONAR"])
                         select row;
            if (filter.Count() > 0)
            {
                tableRelatorio = filter.CopyToDataTable();
                if (caixa_selecao.ShowDialog() == DialogResult.Cancel)
                    return;
                else
                    EtqFraciona.PrinterSettings = caixa_selecao.PrinterSettings;
                EtqFraciona.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Etiqueta", 420, 200);
                etqfrac();
            }
            else
            {
                MessageBox.Show("Selecione pelo menos um registro para impressão!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

        }
    
asked by anonymous 27.07.2017 / 22:33

2 answers

2

I think you're putting this code in the wrong event or method.

Do this:

    private void btnImprimir_Click(object sender, EventArgs e)
    {
        printDocument1.Print();
    }

In the PrintDocument1_PrintPage method do this:

  PrintDocument1_PrintPage(object sender, PrintPageEventArgs e)
  {
        DataTable tableGrid = (DataTable)DGW_EtqFracionamento.DataSource;
        DataTable tableRelatorio = null;

        var filter = from DataRow row in tableGrid.Rows
                     where Convert.ToBoolean(row["SELECIONAR"])
                     select row;
        if (filter.Count() > 0)
        {
            tableRelatorio = filter.CopyToDataTable();
            if (caixa_selecao.ShowDialog() == DialogResult.Cancel)
                return;
            else
                EtqFraciona.PrinterSettings = caixa_selecao.PrinterSettings;

            EtqFraciona.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Etiqueta", 420, 200);
            etqfrac();
        }
        else
        {
            MessageBox.Show("Selecione pelo menos um registro para impressão!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return;
        }
    }
    
28.07.2017 / 04:31
0

Danilo thanks for the attention, but also did not work, I think the problem is in the part that I mount the label and send to the printer, I think in this while here down.

private void etqfrac ()         {             conex.Open ();             SqlCommand command;

        StringBuilder Query = new StringBuilder();
        Query.Append("   SELECT                                                                          ");
        Query.Append("   CAST(0 AS bit) AS SELECIONAR                                                    ");
        Query.Append("   ,SC.C2_NUM AS GUIA                                                              ");
        Query.Append("   ,SB.B1_DESC AS PRODUTO                                                          ");
        Query.Append("   ,SC.C2_XNPAIS [PAÍS ORIGEM]                                                     ");
        Query.Append("   ,CONVERT(VARCHAR(10), CAST( SC.C2_XDTFAB AS DATE),103) AS [DT. FABRICAÇÃO]      ");
        Query.Append("   ,CONVERT(VARCHAR(10), CAST( SC.C2_XDTVALI AS DATE),103) AS [DT. VALIDADE]       ");
        Query.Append("   ,SC.C2_XLOTEF AS [LT. FABRICANTE]                                               ");
        Query.Append("   ,SC.C2_XLOTE AS [LT. INTERNO]                                                   ");
        Query.Append("   ,SB.B1_XDCB AS DCB                                                              ");
        Query.Append("   ,SB.B1_XCAS AS CAS                                                              ");
        Query.Append("   ,CONVERT(VARCHAR(10),CAST(SB.B1_PESO AS NUMERIC(15, 3))) + SB.B1_XSEGUM AS PESO ");
        Query.Append("   ,SC.C2_XNOMFA AS FABRICANTE                                                     ");
        Query.Append("   ,CB.CB0_CODETI AS [COD. BARRAS]                                                 ");
        Query.Append("   FROM SC2020 AS SC                                                               ");
        Query.Append("   INNER JOIN SB1020 AS SB WITH (NOLOCK) ON SB.B1_COD = SC.C2_PRODUTO              ");
        Query.Append("   INNER JOIN CB0020 AS CB WITH (NOLOCK) ON CB.CB0_LOTE = SC.C2_XLOTE              ");
        Query.Append("   WHERE C2_NUM = @C2_NUM                                                          ");

        comando = conex.CreateCommand();
        comando.CommandText = Query.ToString();

        comando.Parameters.Add("@C2_NUM", SqlDbType.VarChar).Value = txtGuiaFrac.Text.Trim();

        SqlDataReader reader = comando.ExecuteReader();
        {
            while (reader.Read())
            {
                etqfracionamento.Add("Produto:" + " " + reader[2].ToString());
                etqfracionamento.Add("Lt. interno:" + " " + reader[7].ToString() + "       " + "Dt. Fabric.:" + " " + reader[4].ToString() + "       " + "Dt. Valid.:" + " " + reader[5].ToString());
                etqfracionamento.Add("Qtda.: " + " " + reader[10].ToString() + "   " + "Origem:" + " " + reader[3].ToString() + " " + "Fabricante: " + " " + reader[11].ToString());
                etqfracionamento.Add("Lt. Fabric.:" + " " + reader[6].ToString() + "    " + " DCB: " + " " + reader[8].ToString() + reader[12].ToString());
                int qtdeCarac = reader[9].ToString().Length;
                int loop = qtdeCarac / 43;
                int pos = 1;
                if (loop == 0)
                {
                    etqfracionamento.Add("CAS:" + " " + reader[9].ToString());
                }
                else
                {
                    for (int i = 1; i <= loop; i++)
                    {
                        if ((pos + 43) > qtdeCarac)
                            etqfracionamento.Add(reader[9].ToString().Substring(pos));
                        else
                            etqfracionamento.Add((i == 1 ? "CAS: " : "     ") + reader[9].ToString().Substring(pos, 43));

                        pos = (i * 43) + 1;
                    }
                }
                etqfracionamento.Add("Guia:" + " " + reader[1].ToString());
                etqfracionamento.Add("Cod:" + " * " + reader[12].ToString() + " * ");
                EtqFraciona.Print();
                etqfracionamento.Clear();
            }
        }
        conex.Close();
    } 
    
28.07.2017 / 14:32