i just started with WPF and the MVVM pattern. I want to access data from a SQL Server, The connection works so far, so thats not the problem. But currently my problem is, that first, I don't know what WPF Control is best to display the Data(ListView, DataGrid, ...?) Second, I just don't get the data displayed in my ListView.
Currently, I have my ViewModel, that looks like this:
namespace StaticIPConfiger.Modelle {
class VModel {
public List<string> AlleKunden {
get {
List<String> customerData = new List<String>();
DatabaseConnection connection = new DatabaseConnection();
SqlCommand getCustomers = new SqlCommand("Select c_name,l_name,a_town,a_pcode,a_street from dbo.AllCustomers", connection.Connection);
connection.Connection.Open();
getCustomers.ExecuteNonQuery();
SqlDataReader reader = getCustomers.ExecuteReader();
if(reader.HasRows) { }
while (reader.Read()) {
customerData.Add(reader.GetString(0));
}
connection.Connection.Close();
return customerData;
}
}
}
The .xaml from the Page where I want to display the data, looks like this.
<Page x:Class="StaticIPConfiger.PageViewCustomers"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:StaticIPConfiger"
xmlns:localvm="clr-namespace:StaticIPConfiger.Modelle"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="500"
Title="PageViewCustomers">
<Page.Resources>
<ObjectDataProvider x:Key="vm" ObjectType="{x:Type localvm:VModel}" />
</Page.Resources>
<Grid DataContext="{Binding}">
<ListView Margin="8" Height="350" Width="500"
ItemsSource="{Binding}" DataContext="{Binding Path=AlleKunden}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=c_name}"
Header="Name" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=l_name}"
Header="Standort" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=a_town}"
Header="Ort" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=a_pcode}"
Header="PLZ" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=a_street}"
Header="Straße" Width="100"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
So it displays the Headers, by doing the Binding : "Binding Path=a_town" I want to refer to the Columns in my View from the SQL Server.
I think, that the DataType of my Method "AlleKunden" is wrong, maybe I dont need a List but another Type?
Another question: I have a dataSet.xsd File in my Solution. Cant I instead access the dataSet instead of the Database? Or do I miss something here?
Thanks already!!
If you need any more code, or have questions, just tell me.
Oh, and a Happy new Year to everyone!!
EDIT: I have one more question for my next step. I want to add a new customer to my database. A customer can have one or more "locations" and every location has a different address. Now my problem is, that in the database these are 3 different tables. customer, location and address. Now if I insert a location, I need to insert a customer ID for that location, same with the address. An Address needs a location id in the database. I thought about first inserting the customer, then get the max(customerid) from the database to insert the location with the max(customerid) and next i have to insert the address by using the max(locationid). Is there a more easy way to accomplish this task, because this seems pretty confusing, and I dont really know how to accomplish it by using wpf and mvvm pattern..