Selection of chekbox in datagridview

Asked

Viewed 48 times

2

Good afternoon. I have a datagridview that I populate through a database select, and in this datagrideviwer I have a column with a chekbox, to select the line I want to send to the print, but the following is happening, when I select the line, and have printed it 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 button code print.

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;
            }

        }

2 answers

2


I believe you are putting this code in the wrong event or method.

Do it like this:

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

In the Printdocument1_printpage method does so:

  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;
        }
    }

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 that while down here.

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();
    } 
  • Problem solved.. Thank you.

  • Comment on how you solved it?

  • So the solution that I found was to put the code that I make the filter inside the method where I do the label printing, then I put the while inside the if that I do the chekbox selection in datagridview, if it is marked it makes the impression of the marked label and put a break in that method, to exit the while, if it does not find marked it makes the complete impression of all the lines of the datagridview.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.