Continuing with the example of Listview show two fields in each row
Visual Studio Community 2015 + SQLite + Entity Framework
The database has 2 fields: ID and ShipType
Class for mapping the database:
namespace ImperialFleet
{
public class ShipTypeClass
{
private string id;
private string type;
public ShipTypeClass(string id, string type)
{
this.Id = id;
this.ShipType = type;
}
public string Id { get; set; }
public string ShipType { get; set; }
}
}
Code to load the Listview
private void button_Click(object sender, RoutedEventArgs e)
{
List<string> shipList = new List<string>();
using (var db = new ImperialFleetDBEntities())
{
shipList = (from g in db.ShipsType select g.ShipType).ToList();
}
listView.Items.Clear();
foreach (string str in shipList)
{
listView.Items.Add(str);
}
xaml Listview
<ListView x:Name="listView" HorizontalAlignment="Left" Height="198" Margin="39,10,0,0" VerticalAlignment="Top" Width="446">
<ListView.View>
<GridView>
<GridViewColumn Header="Id" Width="Auto"
DisplayMemberBinding="{Binding Id}"/>
<GridViewColumn Header="Tipo nave" Width="Auto"
DisplayMemberBinding="{Binding ShipType}"/>
</GridView>
</ListView.View>
</ListView>
I do not know how to take the Id (which is unique in the database) of the selected record in the listview and then delete it from the database, if I have to generate a new instance with the object to delete or if you just need to reference it by your ID in some way
I guess it will be something like that
using (var db = new ImperialFleetDBEntities())
{
db.ShipsType.Remove(IDdelItemABorrar);
db.SaveChanges();
}
but I do not know how to "take" IDdelItemABorrar from the Listview