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

    xaml

    <Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
        <Grid>


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

    xaml.cs




    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()
    {
    InitializeComponent();
    }


    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();
    ad.Fill(dt);
    ListViewEmployeeDetails.DataContext = dt.DefaultView;
    con.Close();
    }
    }
    }


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

Tagged

Popular Posts of the Week

    Optimum Creative