You can try to do the following, get the tables within your dataset
DataTableCollection collection = ds.Tables;
After that you get the DataTable
that contains the data:
DataTable table = collection[0];
even there you have a DataTable
that we will use later.
The example that I will post will not be with all your data but I know it will be understood so that you can reproduce it.
Your datagrid
must be edited to be able to enter data from the code, since you added columns by default you must add a binding to all your columns:
<DataGrid x:Name="dataGridUsuarios" CanUserAddRows="True" HorizontalAlignment="Left" Margin="64,70,0,0" VerticalAlignment="Top" Height="210" Width="364">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=nombre}" ClipboardContentBinding="{x:Null}" Header="nombre" HeaderStringFormat="Nombre" Width="100"/>
<DataGridTextColumn Binding="{Binding Path=apellido}" ClipboardContentBinding="{x:Null}" Header="apellido" HeaderStringFormat="Apellido" Width="100"/>
<DataGridTextColumn Binding="{Binding Path=sexo}" ClipboardContentBinding="{x:Null}" Header="sexo" HeaderStringFormat="Sexo" Width="80"/>
<DataGridTextColumn Binding="{Binding Path=edad}" ClipboardContentBinding="{x:Null}" Header="edad" HeaderStringFormat="Edad" Width="70"/>
</DataGrid.Columns>
</DataGrid>
The parts as Binding="{Binding Path=nombre}"
are important here because thanks to these data will be imported.
After that you must create a class to add your rows with all the data you are showing, in my example I will only work with four: name, last name, sex, age.
Then:
public class Modelo
{
public string nombre { get; set; }
public string apellido { get; set; }
public string sexo { get; set; }
public string edad { get; set; }
}
Then imagine that my datatable
table , previously mentioned has those data, what we should do now is to go through that datatable
to add those contents to our datagrid.
foreach (DataRow row in table.Rows)
{
var data = new Modelo { nombre = row["Nombre"].ToString(), apellido = row["Apellidos"].ToString(), sexo = row["Sexo"].ToString(), edad = row["Edad"].ToString() };
dataGridUsuarios.Items.Add(data);
}
where the row["Nombre"]
are values of your query, you will have ones in this way:
row["idUsuario"]
row["Nombre"]
row["ApellidoP"]
row["ApellidoM"]// y asi con todos tus datos.
After that I would have to give you a result similar to this one.
Also keep in mind that the Path = name of your xaml file must have the same format as your model.
Also, about the id that you want to put, you can do it as you did with the other columns, but since this should not be visible, you can define it with Visibility="Hidden"
. Ex:
<DataGridTextColumn Binding="{Binding Path=id}" ClipboardContentBinding="{x:Null}" Header="id" HeaderStringFormat="id" Visibility="Hidden"/>