I am trying to generate a list from a database that I have and add it to a listbox in WPF C# with LINQ.
This is the XAML I have for now:
<ListView x:Name="ListBox" Margin="16,232,22,10.4" SelectionMode="Multiple">
<ListView.View>
<GridView>
<GridViewColumn Header="Nettstasjon" Width="100" DisplayMemberBinding="{Binding Path=Name}"/>
<GridViewColumn Header="Område" Width="100" DisplayMemberBinding="{Binding Path=Area}"/>
<GridViewColumn Header="Radial" Width="100" DisplayMemberBinding="{Binding Path=Radial}"/>
</GridView>
</ListView.View>
</ListView>
And to read the database I have this codebehind. I made a class "TransformerStation" to hold the data per line.
public class TransformerStation : INotifyPropertyChanged
{
public string Name { get; set; }
public string Radial { get; set; }
public string Area {get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
Then I made a class to read the database
public IEnumerable<TransformerStation> ReadCSV(string fileName, string radial)
{
string[] lines = File.ReadAllLines(System.IO.Path.ChangeExtension(fileName, ".csv"), Encoding.UTF8);
return lines.Select(line =>
{
string[] data = line.Split(';');
foreach (var value in data)
{
if (data = radial)
{
return new TransformerStation(data[0], data[1], data[2]);
}
}
});
}
My database looks like this:
N765;TANGEN;TANGEN L98
R2351;SPIKKESTAD;SPIKKESTAD K88
S622;KRÅKSTAD;KRÅKSTAD L812
S1318;KRÅKSTAD;KRÅKSTAD L812
What I need is as follows:
I have a WPF with butttons with content that is equal to the third column in my database(F.eks KRÅKSTAD L812). I want, when I push a button, to collect all the items in column one from my database, that matches the button content in column three, and show them in a listbox that I have created above.
So, if I push the button with content "Kråkstad L812" then my list will show
S622
S1318
I am sorry, I am really new at C# and WPF. So I dont have much code to show. But I really appreciate all the help I can get and it really makes a difference if you explain what is happening in the code :) So just to clarify, I basically need the SELECT radial FROM database and generate a list from that.
EDIT, I am also open to other ways of importing the data from the csv database. I have just picked one that worked for me :)