C # Simple - Forms - Move to the datagrid which I select in the combobox [closed]

1

Well, I'm new to programming language, I need some help. I'm trying to call the database in combobox with select, but I'm not able to do a function that when I click OK, it shows me the answer of what I selected. I need that help.

using System;
using System.Data.OleDb;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{


    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //Instanciando conexao e comand do SQL
        SqlConnection sql = new SqlConnection("Password=xxxxxxxxxx;Persist Security Info=True;User ID=xxxxxxInitial Catalog=BD_Testes;Data Source=xx.xxx.xx.xx");
        SqlCommand comand = null;
        SqlDataAdapter adapter = null;

        //Dataset é uma tabela em memoria
        DataSet ds = new DataSet();


        //Abrindo conexao com o banco
        sql.Open();

        //Defino o comando que será executado
        comand = new SqlCommand();
        comand.CommandText = "select ccNomeFisicoArquivo FROM tbArquivoImportado";
        comand.Connection = sql;

        //Instancio o adapter, passando comand como parametro.
        //O resultado do select será atribuido ao adapter, que irá preencher o dataset com o resultado
        //Dataset é uma tabela que fica na memoria.
        adapter = new SqlDataAdapter(comand);
        adapter.Fill(ds);

        //Para cada linha da tabela no dataset, preencho o combobox
        foreach (DataRow row in ds.Tables[0].Rows)
        {

            comboBox1.Items.Add(row["ccNomeFisicoArquivo"]);

        }

        //Sempre fechar a conexao
        sql.Close();
    }


    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }



    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

        List<int> listaDosIdsNaGrid = new List<int>();

        //AQUI EU PASSO P GRID O ITEM DA COMBO
        dataGridView1.Rows.Add(comboBox1.SelectedText);
        listaDosIdsNaGrid.Add((int)comboBox1.SelectedValue);



    }

    private void button1_Click(object sender, EventArgs e)
    {

    }

    private void button1_Click_1(object sender, EventArgs e)
    {

    }



    }
}

The combobox is funfando, I need to get the item from the combobox and when I click "OK" it will appear the result of the select that I give in the item

    
asked by anonymous 11.08.2014 / 19:47

2 answers

2

My friend, so I understand that getting the selected text in the Combobox would be the selecteditem

see an example:

private void showSelectedButton_Click(object sender, System.EventArgs e) {
    int selectedIndex = comboBox1.SelectedIndex;
    Object selectedItem = comboBox1.SelectedItem;

    MessageBox.Show("Selected Item Text: " + selectedItem.ToString() + "\n" +
                    "Index: " + selectedIndex.ToString());
}
    
11.08.2014 / 20:46
1

Well I do not quite understand, but I think you want the datagrid content in the textbox and so on? if. put the following line in the double clik event or in the clik on the datagrid.

string strSelect;
// vamos obter a célula atual (que possui o foco)
DataGridViewCell celulaAtual = dgvProcurar.CurrentCell;
// vamos exibir o valor da célula atual
string valor = celulaAtual.Value.ToString();
strSelect = "SELECT campos FROM tabela WHERE campo LIKE '" + valor + "' ORDER BY campo";.
//agora buscamos as informações na tabela do banco
DataTable tabela;
tabela = conexao.ExecultarSelect(_sql);
if ((tabela == null) || (tabela.Rows.Count == 0))
{
    MessageBox.Show("Registros não encontrados", "Título", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
    textboc.Text = tabela.Rows[0]["Campo"].ToString();
    //Assim por diante
}
    
11.08.2014 / 20:39