Good day, I have had problems to show the data in a grid using wpf the code does not throw me error and reviewing the step by step I see that the methods take the values registered in the DB but do not take them to the grid. I share code:
1.View
<DataGrid.Columns>
<DataGridTextColumn Header="Inspección #" Binding="{Binding Path=numeroInspeccion}" Visibility="Visible" Width="*" IsReadOnly="False"/>
<DataGridTextColumn Header="Vehiculo" Binding="{Binding Path=vehiculo}" Visibility="Visible" Width="*" IsReadOnly="False"/>
<DataGridTextColumn Header="Conductor" Binding="{Binding Path=conductor}" Visibility="Visible" Width="*" IsReadOnly="False"/>
<DataGridTextColumn Header="Fecha" Binding="{Binding Path=fecha}" Visibility="Visible" Width="*" IsReadOnly="False" />
<DataGridTextColumn Header="Inspector" Binding="{Binding Path=inspector}" Visibility="Visible" Width="*" IsReadOnly="False"/>
</DataGrid.Columns>
</DataGrid>
2.Model
public DataTable CargarInspeccion()
{
DataTable dt = new DataTable("CargarInspeccion");
SqlConnection SqlCon = new SqlConnection();
try
{
SqlCon.ConnectionString = Conexion.Cn;
//SqlCon.Open();
//Código con sentencia
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = SqlCon;
sqlCmd.CommandText = "PRC_CARGAR_INSPECCION";
sqlCmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter SqlDat = new SqlDataAdapter(sqlCmd);
SqlDat.Fill(dt);
}
catch(Exception e)
{
dt = null;
}
return dt;
}
3.Vista Modelo
private void CargarDatosInspeccion()
{
List<EstructuraDtg> obj = new List<EstructuraDtg>();
foreach(DataRow dr in Operaciones.CargarInspecciones().Rows)
{
obj.Add(new EstructuraDtg { NumeroInspeccion = dr[0].ToString(), Vehiculo = dr[1].ToString(), Conductor = dr[2].ToString(), Fecha = dr[3].ToString(), Inspector = dr[4].ToString() });
}
this.DtInspeccion.ItemsSource = obj;
}