Hello everyone!
I'm a newbie WPF. Im building a project use 3-tier to connect Database SQL, Insert, update data. But in the first, fill database and show it to Listview is not work!
This is my code:
code XAMP:
[CODE]
<Window x:Class="vanngoi_test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="524"
Width="640" Loaded="Window_Loaded"
DataContext="{Binding}">
<Grid>
<ListView
Name="ListViewCustomerDetails" ItemsSource="{Binding
Path=vanngoi_test}" Margin="45,51,200,126" >
<ListView.Background>
<LinearGradientBrush>
<GradientStop Color="Wheat" Offset="0"/>
</LinearGradientBrush>
</ListView.Background>
<ListView.View>
<GridView x:Name="Grid">
<GridViewColumn Header="Name"
DisplayMemberBinding="{Binding Path=name}"/>
<GridViewColumn Header="Account Balance"
DisplayMemberBinding="{Binding Path=accbalance}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
[/CODE]
I used 3-tier (Presentation,Business,DataAccess)
This is class Connect.cs in DataAccess
[CODE]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace vanngoi_test.DataAccess
{
class Connect
{
public
SqlConnection conn;
public
SqlDataAdapter dataAd;
//public
DataTable dataTable;
public DataSet
ds;
public void
ConnectData()
{
String
strConn = @"Data Source=GIANG-PC\SQLEXPRESS;Initial
Catalog=vanngoi_test;Integrated Security=True";
try
{
conn =
new SqlConnection(strConn);
conn.Open();
conn.Close();
}
catch
(Exception ex)
{
MessageBox.Show("Error" + ex.Message);
}
}
// M?c
dích là l?y d? li?u t? câu truy v?n truy?n vào r?i load lên listview
// get data from query and load to listview
public
DataSet GetDataSet(string sql)
{
dataAd
= new SqlDataAdapter(sql, conn);
ds =
new DataSet();
dataAd.Fill(ds);
return
ds;
}
}
[/CODE]
this code class customersBUS.cs in Business:
[CODE]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using vanngoi_test.DataAccess;
namespace vanngoi_test.Business
{
class khachhangBUS
{
Connect
connectData = new Connect();
public DataSet
GetInfoCustomers()
{
string sql
= "SELECT name, accbalance FROM inforcustomers";
return
connectData.GetDataSet(sql);
}
}
}
[/CODE]
It connected with SQL server, but show to listview is not work. Follow XAML code in MainWindow.xaml.cs
[CODE]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
using vanngoi_test.Business;
using vanngoi_test.DataAccess;
namespace vanngoi_test
{
///
<summary>
/// Interaction
logic for MainWindow.xaml
///
</summary>
public partial
class MainWindow : Window
{
public
MainWindow()
{
InitializeComponent();
}
private void
Window_Loaded(object sender, RoutedEventArgs e)
{
//Connect
cn = new Connect();
ListViewCustomerDetails.ItemsSource = cn.ds.Tables[0].DefaultView;
}
}
}
[/CODE]
please give me solution!
Thanks all!