Filling out a form’s textbox with information from a datagridview

Asked

Viewed 328 times

1

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

As I show in the image below.

inserir a descrição da imagem aqui

also follows my code

public partial class frmFaturaAnual : Form
{

    SqlConnection conexao = null;
    SqlCommand comando = null;

    public frmFaturaAnual()
    {
        InitializeComponent();
    }

    private void ListaGrid()
    {
        string strSQL = @"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();
    }
}

2 answers

1


Whereas, you will use the Doubleclick event in datagridview, and that will always open a new form, when the event occurs:

in your frmQtdaCliente, create two properties

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

in the form load event, fill in the textbox.

to call the form, in the event dgAnual_CellDoubleClick:

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();

  • Rovann even gave his procedure, but in txtMes I need the name of Mes being filled in txtbox, the way this code is bringing txtMes the cell content, not and that I want, but the name of the column I am selecting the cell.

  • When filling the month variable, use dgAnual.Colums[e. Columnindex]. Name

  • Sorry if you have an error in the syntax, I’m on mobile, if it helped, mark as answer please

  • Thank you gave it right here vlw

  • Now I have another mistake in some columns when I cliko. An untreated "System.Data.Sqlclient.Sqlexception" exception occurred in System.Data.dll Additional Information: Divide by zero error encountered.

  • then we have to see where exactly happens the other error, whether it is in this first form, or the other, Divide by zero is happening in some calculation of an sql command. In the double click event, you should put the "if(e.Rowindex < 0 || e.Columnindex < 0) Return;" chunk to avoid entering the code if it is clicked on a header. Please mark as answer

  • Good morning Rovann. Funny that only happens in the January month column of datagriview, the other columns run normally.

  • possibly by the value 0 (zeroed) that tries to perform the division, show the sql syntax to try to help you, if possible the code of the other form

  • friend, you marked the other member’s answer as your question !? Not that his is wrong, it also works, but check mine too

  • I’m sorry I think I’ve scored right now.

Show 5 more comments

1

There are several ways to do this. I almost always opt for the idea of sending the data via 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

  1. Add an event from click (or double click) in the DataGridView (not in cells - because the event is fired anywhere on the line that is clicked, if you do not want this use the event CellDoubleClick even);

  2. Capture the necessary information;

  3. Pass this information as a parameter in the new constructor 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();
}

Browser other questions tagged

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