how to fill listview from database in wpf
  • wpf fill listview from datatable or database use below code.


    <Window x:Class="WpfApplication1.Window1"
        Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">

            <ListView Name="ListViewEmployeeDetails" Margin="4,20,40,100" ItemTemplate="{DynamicResource EmployeeTemplate}" ItemsSource="{Binding Path=Table}">
                        <GradientStop Color="White"  Offset="0"/>
                        <GridViewColumn Header="Country ID" DisplayMemberBinding="{Binding Path=CountryId}"/>
                        <GridViewColumn Header="Country Name" DisplayMemberBinding="{Binding Path=CountryName}"/>


    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.Configuration;
    using System.Data;
    using System.Data.SqlClient;

    namespace WpfApplication1
    /// Interaction logic for Window1.xaml
    public partial class Window1 : Window
    public Window1()

    private void Window_Loaded(object sender, RoutedEventArgs e)
    SqlConnection con = new SqlConnection();
    SqlDataAdapter ad = new SqlDataAdapter();
    SqlCommand cmd = new SqlCommand();
    string str = "SELECT CountryId, CountryName FROM tblCountry";
    cmd.CommandText = str;
    ad.SelectCommand = cmd;
    con.ConnectionString = @"Data Source=.\SqlExpress; Initial Catalog=dbTest;Integrated Security=True";
    cmd.Connection = con;
    DataTable dt = new DataTable();
    ListViewEmployeeDetails.DataContext = dt.DefaultView;

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook


Popular Posts of the Week