locked
How to Bind DataTable to ListView RRS feed

  • Question

  • I have a ListView

          <ListView Name="listView" Margin="5,5,5,5" > 
          </ListView> 

    and C#
                DataTable table = ds.Tables["Students"]; 
                Binding bind = new Binding(); 
                listView.DataContext = table.DefaultView; 
                listView.SetBinding(ListView.ItemsSourceProperty, bind); 
     

    But result is only
    System.Data.DataRowView
    System.Data.DataRowView
    System.Data.DataRowView
    instead of real values.

    How can I bind DataTable to a ListView when I don't know what columns does DataTable has?


    • Edited by Lucky7777 Thursday, November 13, 2008 8:16 AM
    Thursday, November 13, 2008 8:15 AM

Answers

  • Hi,

    The ListView does not include any mechanisms for analysing the bound data and creating a suitable layout. Typically you would add  GridView as the View type for your ListView, then manually add columns to the GridView.

    If you want to have automatic column generation from a DataTable you should try using the WPF DataGrid. You can download it from here:

    http://www.codeplex.com/wpf

    You might also like this article:

    http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx

    Colin E.
    • Proposed as answer by Colin Eberhardt Thursday, November 13, 2008 9:52 AM
    • Marked as answer by Lucky7777 Friday, November 14, 2008 3:44 AM
    Thursday, November 13, 2008 9:51 AM
  •  Of course it is possible to use the DataGrid. But it is also possible to use the ListView, except that it cannot create the columns automatically.

    Below you find some sample code how I use it with DataTables.

    Regards
    Norbert Ruessmann

    Window1.xaml


    <Window x:Class="WpfListView.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">  
        <Grid> 
            <ListView ItemsSource="{Binding}">  
                <ListView.View> 
                    <GridView AllowsColumnReorder="True">  
                        <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" /> 
                        <GridViewColumn DisplayMemberBinding="{Binding Date}" Header="Last Name"/>  
     
                    </GridView> 
                </ListView.View> 
            </ListView> 
        </Grid> 
    </Window> 
     

    Window1.xaml.cs

    using System;  
    using System.Data;  
    using System.Windows;  
     
    namespace WpfListView  
    {  
        /// <summary>  
        /// Interaction logic for Window1.xaml  
        /// </summary>  
        public partial class Window1 : Window  
        {  
            private DataSet _ds ;  
            public Window1()  
            {  
                InitializeComponent();  
            }  
     
            protected override void OnInitialized(EventArgs e)  
            {  
                base.OnInitialized(e);  
                _ds = new DataSet();  
                DataTable table = new DataTable();  
                _ds.Tables.Add(table);  
                DataColumn col = new DataColumn("Name"typeof(string));  
                table.Columns.Add(col);  
                col = new DataColumn("Date"typeof(DateTime));  
                table.Columns.Add(col);  
     
                DataRow row = table.NewRow();  
                row["Name"] = "John Miller";  
                row["Date"] = DateTime.Now;  
                table.Rows.Add(row);  
     
                row = table.NewRow();  
                row["Name"] = "Tom Smith";  
                row["Date"] = DateTime.Now - TimeSpan.FromDays(10);  
                table.Rows.Add(row);  
     
                this.DataContext = _ds.Tables[0].DefaultView;  
            }  
        }  
    }  
     
    • Marked as answer by Lucky7777 Friday, November 14, 2008 3:43 AM
    Thursday, November 13, 2008 10:28 AM

All replies

  • Hi,

    The ListView does not include any mechanisms for analysing the bound data and creating a suitable layout. Typically you would add  GridView as the View type for your ListView, then manually add columns to the GridView.

    If you want to have automatic column generation from a DataTable you should try using the WPF DataGrid. You can download it from here:

    http://www.codeplex.com/wpf

    You might also like this article:

    http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx

    Colin E.
    • Proposed as answer by Colin Eberhardt Thursday, November 13, 2008 9:52 AM
    • Marked as answer by Lucky7777 Friday, November 14, 2008 3:44 AM
    Thursday, November 13, 2008 9:51 AM
  •  Of course it is possible to use the DataGrid. But it is also possible to use the ListView, except that it cannot create the columns automatically.

    Below you find some sample code how I use it with DataTables.

    Regards
    Norbert Ruessmann

    Window1.xaml


    <Window x:Class="WpfListView.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">  
        <Grid> 
            <ListView ItemsSource="{Binding}">  
                <ListView.View> 
                    <GridView AllowsColumnReorder="True">  
                        <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" /> 
                        <GridViewColumn DisplayMemberBinding="{Binding Date}" Header="Last Name"/>  
     
                    </GridView> 
                </ListView.View> 
            </ListView> 
        </Grid> 
    </Window> 
     

    Window1.xaml.cs

    using System;  
    using System.Data;  
    using System.Windows;  
     
    namespace WpfListView  
    {  
        /// <summary>  
        /// Interaction logic for Window1.xaml  
        /// </summary>  
        public partial class Window1 : Window  
        {  
            private DataSet _ds ;  
            public Window1()  
            {  
                InitializeComponent();  
            }  
     
            protected override void OnInitialized(EventArgs e)  
            {  
                base.OnInitialized(e);  
                _ds = new DataSet();  
                DataTable table = new DataTable();  
                _ds.Tables.Add(table);  
                DataColumn col = new DataColumn("Name"typeof(string));  
                table.Columns.Add(col);  
                col = new DataColumn("Date"typeof(DateTime));  
                table.Columns.Add(col);  
     
                DataRow row = table.NewRow();  
                row["Name"] = "John Miller";  
                row["Date"] = DateTime.Now;  
                table.Rows.Add(row);  
     
                row = table.NewRow();  
                row["Name"] = "Tom Smith";  
                row["Date"] = DateTime.Now - TimeSpan.FromDays(10);  
                table.Rows.Add(row);  
     
                this.DataContext = _ds.Tables[0].DefaultView;  
            }  
        }  
    }  
     
    • Marked as answer by Lucky7777 Friday, November 14, 2008 3:43 AM
    Thursday, November 13, 2008 10:28 AM