Filling in textbox of a form with information from a datagridview

1

I have a datagrideview on a form, and I need it when I click on the datagridview cell it fills two fields in another form.

As I show in the image below.

Alsofollowmycode

publicpartialclassfrmFaturaAnual:Form{SqlConnectionconexao=null;SqlCommandcomando=null;publicfrmFaturaAnual(){InitializeComponent();}privatevoidListaGrid(){stringstrSQL=@"SELECT 
                          RTRIM(B1_COD) + ' - ' + B1_DESC AS PRODUTO, 
                          [Janeiro], [Fevereiro], [Março], [Abril], [Maio], [Junho], [Julho], 
                          [Agosto], [Setembro], [Outubro], [Novembro], [Dezembro] 
      FROM ( SELECT SB.B1_COD, SB.B1_DESC, m.nm_mes, SD.D2_QUANT FROM  SD2010 AS SD left outer join mes m on m.cd_mes = month(SD.D2_EMISSAO) 
      INNER JOIN SB1010 AS SB WITH (NOLOCK) ON SB.B1_COD = SD.D2_COD 
      WHERE SD.D_E_L_E_T_ <> '*' 
      AND SD.D2_CF IN ('5102', '5117', '5119', '5123', '5124', '5403', '5405', '6102', '6108', '6110', '6117', '6119', '6123', '6124', '6403', '6405', '7102') 
      AND YEAR(SD.D2_EMISSAO) = '" + txtAno.Text +"' ) AS F PIVOT (SUM(D2_QUANT) FOR nm_mes IN  ([Janeiro], [Fevereiro], [Março], [Abril], [Maio], [Junho], [Julho], [Agosto], [Setembro], [Outubro], [Novembro], [Dezembro])) AS P";

        conexao = new SqlConnection(conm);
        comando = new SqlCommand(strSQL, conexao);

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

            dgAnual.DataSource = dtLista;
        }
        catch
        {
            MessageBox.Show("Não existem dados a serem encontrados");
        }
    }

    private void btnPesquisar_Click(object sender, EventArgs e)
    {
        ListaGrid();
    }

    private void btnFechar_Click(object sender, EventArgs e)
    {
        Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        frmImpFatAnual imp = new frmImpFatAnual(txtAno.Text);
        imp.Show();
    }

    private void dgAnual_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    { 

        frmQtdaCliente custo = new frmQtdaCliente(txtAno.Text);
        custo.Show();
    }
}
    
asked by anonymous 11.04.2017 / 21:11

2 answers

1

Considering that you will use the doubleclick event in the datagridview, and that a new form will always be opened when the event occurs:

In your customer context, create two properties

public string Mes {get;set;}
public string Produto {get;set;}

In the form's load event, fill in the textbox.

to call the form, in the dgAnual_CellDoubleClick event:

frmQtdaCliente custo = new frmQtdaCliente(txtAno.Text);
custo.Mes= dgAnual.Rows[e.RowIndex].Cell["nome da sua coluna mes"].Value.ToString();
custo.Produto = dgAnual.Rows[e.RowIndex].Cell["nome da sua coluna produto"].Value.ToString();

cost.Show ();

    
11.04.2017 / 21:45
1

There are several ways to do this. I almost always opt for sending the data via the constructor to the new form , this makes it easy to put values in the controls right after they are created and also prevents these controls from being accessed outside the form .

My tip is basically based on three things to do

  • Add a click event (or double click ) in DataGridView (not in cells - because the event is fired anywhere on the line is clicked, if you do not want this use the event CellDoubleClick same);

  • Capture the required information;

  • Pass this information as a parameter in the constructor of the new form .

    Example:

    private void dgAnual_DoubleClick(object sender, DataGridViewCellMouseEventArgs e)
    { 
        if(e.RowIndex < 0 || e.ColumnIndex < 0)
            return;
    
        var row = dgAnual.Rows[e.RowIndex];
        var nomeProduto = row["NomeDaColunaProduto"].Value;
        var mes = row["NomeDaColunaMes"].Value;
    
        FormNovo form = new FormNovo(nomeProduto, mes);
        form.Show();
    }
    
        
  • 11.04.2017 / 21:48