I want to obtain the value of the cell idUsuario that has the following configuration 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. By means of a button, the id of that record will be obtained to use it later.
the filling code 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 DataGridFillTest has 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; }
}
Since I was helping you in the course of this problem, I will continue with it.
You can get it in various 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:
In which you add its content, which according to your model should go as follows:
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 full function:
This event will fire when left clicking on a row.
Personally, I always implement both, I recommend you do the same to avoid failures, in addition to giving the end user more ease of use.
greetings in your case you can do the following to obtain the selected user in the DataGrid.
Having the user you can already have the value of each property for example
Try to see if you can fix it that way.