How to set the data of a Datagrid to a Textbox in C#

Asked

Viewed 4,412 times

2

I am trying to set the data of the selected Datagrid line, which comes from B.D., to show in each Textbox. The data of each column of the selected row goes to a Textbox. How do I? Which method to use?

namespace EstoquePeca
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        txtPeca.Focus();
    }
    //Consultar Todos
    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        OleDbConnection aConnection = new    OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/estoque.mdb");
        OleDbCommand aCommand = new OleDbCommand("select * from ESTOQUEPECA", aConnection);
        aConnection.Open();
        var Consulta = aCommand.ExecuteReader();

        GridDados.ItemsSource = Consulta;

        GridDados.Columns[0].Header = "Código";
        GridDados.Columns[1].Header = "Quantidade";
        GridDados.Columns[2].Header = "Descrição";
        GridDados.Columns[3].Header = "Alternativo 1";
        GridDados.Columns[4].Header = "Alternativo 2";
    }
    //Consultar pelo código
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        string sql = "select * from ESTOQUEPECA WHERE COD ='" + txtPeca.Text + "'";
        OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/estoque.mdb");
        OleDbCommand aCommand = new OleDbCommand(sql, aConnection);
        aConnection.Open();
        var Consulta = aCommand.ExecuteReader();
        try
        {
            if (txtPeca.Text.Equals(""))
            {
                MessageBox.Show("Digite o código da Peça!");
            }
            else
            {
                GridDados.ItemsSource = Consulta;
                GridDados.Columns[0].Header = "Código";
                GridDados.Columns[1].Header = "Quantidade";
                GridDados.Columns[2].Header = "Descrição";
                GridDados.Columns[3].Header = "Alternativo 1";
                GridDados.Columns[4].Header = "Alternativo 2";
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Erro: " + ex.Message);
        }
    }

    private void Button_Click_4(object sender, RoutedEventArgs e)
    {
        AtualizaPeca ap = new AtualizaPeca();
        ap.Show();
    }
    //Seta os dados no textbox
    private void Button_Click_5(object sender, RoutedEventArgs e)
    {
        AtualizaPeca ap = new AtualizaPeca();
        ap.Show();
        ap.txtCodigo.Text = GridDados.SelectedItem.ToString();
    }

}
  • 1

    What have you done? Can you put some of your source code? So we can understand what has already been done, and help you better.

4 answers

1

I went through a similar situation in a project, but in it every record of my Datagridview represented an object of a specific class. You can create a method Cellcontentclick and in it recover the value of the clicked cell and/or the values of the same record. Follow an example:

    private void meuGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        // Recupera o valor da célula clicada.
        textBox.Text = meuGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
        // Obtém um objeto a partir da linha da célula clicada.
        objMinhaClasse = meuGrid.Rows[e.RowIndex].DataBoundItem as MinhaClasse;
    }
  • I will follow this example, I think I can find a solution. Thank you for now!

1

I did that a little while ago..

The difference is I’d sweep the DataGridView and that’s not your problem, okay? From what I understand you want to click on a line and recover the value of this... I’m sure?

If yes, use the event that "Maniero" showed in the post above with something like this:

DataGridViewCell valor = null;
valor = linha.Cells["colValor"];

Where "colValor" is one of the columns you want to recover the data from.

Once that’s done, try something like TextBox txtValor = valor.Value.ToString();

I think it’s right there! =)

Repeat this procedure for all columns you want to recover the values.

And yes, this is the same implementation of "Maniero", but I think declaring a Datagridviewcell helps a lot for you to read the code. I don’t know rsrs.

Edit: To linha you recover with DataGridViewRow linha = grid.Rows[e.RowIndex], where e is one of the arguments received by the event CellContentClick, as quoted by "Maniero".

0

 private void dataListar_DoubleClick(object sender, EventArgs e)
        {
            this.txtIdUsuario.Text = Convert.ToString(this.dataListar.CurrentRow.Cells["idUsuario"].Value);
            this.txtNomeUsuario.Text = Convert.ToString(this.dataListar.CurrentRow.Cells["nomeUsuario"].Value);            
            this.txtLoginUsuario.Text = Convert.ToString(this.dataListar.CurrentRow.Cells["loginUsuario"].Value);
            this.txtSenhaUsuario.Text = Convert.ToString(this.dataListar.CurrentRow.Cells["senhaUsuario"].Value);
            this.cbNivelAcesso.Text = Convert.ToString(this.dataListar.CurrentRow.Cells["nivelAcesso"].Value);
            this.cbSituacao.Text = Convert.ToString(this.dataListar.CurrentRow.Cells["situacaoUsuario"].Value);
            this.txtDataUsuario.Text = Convert.ToString(this.dataListar.CurrentRow.Cells["dataUsuario"].Value);

            this.tabControl1.SelectedIndex = 1;
        }

0

I was doing a service registration system and had to assign all values that were on the grid to textbox’s, so when I click on a grid, it fills all Textbox’s. I recommend using the Datagrid Mousedoubleclick Event. You can read this example

    TextBox.Text = DataGridView..CurrentRow.Cells[0].Value.ToString();

Browser other questions tagged

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