C# Simple - Forms - Pass to the datagrid what I select in the combobox

Asked

Viewed 1,997 times

1

Guys, good afternoon, I’m new to programming language, I need a little help. I’m able to call the database data in the 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 ta funfando, I need to get the combobox item and when I click "OK" it will appear the result of the select I give in the item

  • Show the result where? I think that’s what was unclear.

2 answers

2

Friend, from what I understood you that to pick the selected text in the Combobox this 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());
}

1

Well I don’t quite understand it, but I think you want the content of the datagrid in the textbox and this? if it is. place the following line in the double clik event or clik in 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
}
  • Well, I’ll try to explain better... With this code, I play on the combobox, what I have in the database... Everything I have on ccNomeFisicoFile, he plays on the combobox. //For each row of the table in the dataset, fill in the combobox foreach (Datarow Row in ds.Tables[0].Rows) { comboBox1.Items.Add(Row["ccNomeFisicoFile"]); } when I select, I will press "OK", and it has to bring me the values of the bank line in which that name is...

  • I was able to play in Datagrid... the way Geilton helped, but since I’m N'Oob, I put it another way to better understand... Datatable dt = new Datatable(); dt.Columns.Add("Index"); dt.Columns.Add("File Name"); dt.Columns.Add("Item 3"); Datarow dr; dr = dt.Newrow(); dr[0] = comboBox1.Selectedindex; dr[1] = comboBox1.Selecteditem; // dr[2] = dt.Rows.Add(dr); dataGridView1.Datasource = dt;

Browser other questions tagged

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