How do I set the data of a DataGrid to a TextBox in C #

2

I'm trying to set the data in the selected row of the DataGrid, 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 do? 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();
    }

}
    
asked by anonymous 31.01.2014 / 21:10

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 CellContentClick method and retrieve the value of the clicked cell and / or values from the same record. Here is 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;
    }
    
31.01.2014 / 22:04
1

Oops! I did this a while back ..

The difference is that I've swept the whole DataGridView and that's not your problem, right? From what I understand you want to click on a line and retrieve the value of this ... am I right?

If yes, use the event that "bigown" 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 retrieve.

Once this is done, try something like TextBox txtValor = valor.Value.ToString();

I think that's right! =)

Repeat this procedure for all columns that you want to retrieve values from.

And yes, this is the same implementation of "bigown", but I think declaring a DataGridViewCell helps a lot for you to read the code .. I do not know.

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

    
27.02.2014 / 15:03
0

I was doing a service registration system and had to assign all values that were on the grid to the textbox's, so when I click on a grid, it fills in all TextBox's. I recommend that you use the DataGrid MouseDoubleClick Event. You can follow this example

    TextBox.Text = DataGridView..CurrentRow.Cells[0].Value.ToString();
    
07.05.2014 / 18:31
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;
        }
    
17.07.2016 / 03:01