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.
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();
}
}
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.
– Junior Guerreiro
When filling the month variable, use dgAnual.Colums[e. Columnindex]. Name
– Rovann Linhalis
Sorry if you have an error in the syntax, I’m on mobile, if it helped, mark as answer please
– Rovann Linhalis
Thank you gave it right here vlw
– Junior Guerreiro
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.
– Junior Guerreiro
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
– Rovann Linhalis
Good morning Rovann. Funny that only happens in the January month column of datagriview, the other columns run normally.
– Junior Guerreiro
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
– Rovann Linhalis
friend, you marked the other member’s answer as your question !? Not that his is wrong, it also works, but check mine too
– Rovann Linhalis
I’m sorry I think I’ve scored right now.
– Junior Guerreiro