How to get the value of a cell from a DataGrid?

2

I want to get the value of the UserID cell that has the following settings made with Visual Studio 2017 in a window (WPF)

<DataGridTextColumn Binding="{Binding Path=idUsuario}" ClipboardContentBinding="{x:Null}"  Header="idUsuario" HeaderStringFormat="idUsuario" Visibility="Hidden"/>

Since the user does not have to see the id I put it hidden, the user selects a row that has the registration data. Through a button you will get the id of that record to use it later.

the code of the filling is the following made with C #

public void llenadoDataGrid()
    {
        String consulta = "select idUsuario,Nombre,ApellidoP ,ApellidoM ,Sexo,Telefono,Edad,Puesto from usuarios;";
        SqlDataAdapter dataAdapter = new SqlDataAdapter(consulta, new BaseDeDatos().obtenerConexion());
        DataSet ds = new DataSet();
        dataAdapter.Fill(ds);
        DataTableCollection collection = ds.Tables;
        DataTable table = collection[0];

        foreach (DataRow row in table.Rows)
        {
            var data = new PruebaDeLLenadoDataGrid {idUsuario = row["idUsuario"].ToString(), Nombre = row["Nombre"].ToString(),
                ApellidoP = row["ApellidoP"].ToString(), ApellidoM = row["ApellidoM"].ToString(),
                Sexo = row["Sexo"].ToString(), Telefono = row["Telefono"].ToString(),
                Edad = row["Edad"].ToString(), Puesto = row["Puesto"].ToString() };
            dataGridUsuarios.Items.Add(data);
        }
    }

The other class I use called TestDataGridDataGrid have the following

lass PruebaDeLLenadoDataGrid
{
    public String idUsuario { get; set; }
    public String Nombre { get; set; }
    public String ApellidoP { get; set; }
    public String ApellidoM { get; set; }
    public String Sexo { get; set; }
    public String Telefono { get; set; }
    public String Edad { get; set; }
    public String Puesto { get; set; }

}
    
asked by Richard Yordy 08.04.2018 в 09:13
source

2 answers

2

As I was helping you in the course of this problem I will continue with it.

You can get it in several ways.

1.- With the keyUp event: you can add it in the function properties of your dataGrid:

Which will auto create a function like this:

private void dataGridUsuarios_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
}

In which you add your content, which according to your model should go as follows:

switch (e.Key)
{
    case Key.Down:
    case Key.Up:
    PruebaDeLLenadoDataGrid dato= (PruebaDeLLenadoDataGrid)dataGridUsuarios.SelectedItem;
    MessageBox.Show("El id es:"+dato.idUsuario+" y el nombre es:"+dato.Nombre);
}

This function captures the selected row with the up and down keys.

2.- with the MouseLeftButtonUp event:

Which in the same way as the previous one will create a function for you, and you add the logic. This would be your complete function:

private void dataGridUsuarios_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    PruebaDeLLenadoDataGrid dato= (PruebaDeLLenadoDataGrid)dataGridUsuarios.SelectedItem;
    MessageBox.Show("El id es:"+dato.idUsuario+" y el nombre es:"+dato.Nombre);
}

This event will be activated by left clicking on a row.

Personally I always implement both, I recommend you do the same to avoid faults, in addition to giving more ease of use to the end user.

    
answered by 08.04.2018 / 14:58
source
1

Greetings in your case you can do the following to get the user selected in the DataGrid.

var seleccionado = dataGridUsuarios.SelectedCells[0].Item as PruebaDeLLenadoDataGrid;

Having the user you can already have the value of each property for example

string idUsuario = PruebaDeLLenadoDataGrid.IdUsuario;

Try to see if so you can solve it.

    
answered by 19.04.2018 в 20:55