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;
}
}
Problem solved.. Thank you.
– Junior Guerreiro
Comment on how you solved it?
– Danillo Victtor
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.
– Junior Guerreiro