How to add rows programatically to a ListView in WPF ?

Hello,

I am a total wpf newbie and played a bit...

why can`t I read the name bill gates sillicon valley in the first Row?

What I read is 3 times this: System.Data.DataRowView
private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DataTable dt = new DataTable();
 
            dt.Columns.Add();
            dt.Columns.Add();
            dt.Columns.Add();
 
            Binding bind = new Binding();            
 
            bind.Source = dt;
            myList.SetBinding(ListView.ItemsSourceProperty, bind);
 
            dt.Rows.Add("bill", "gates", "silicon valley");          
          
        }
 
XAML:
 
<Window x:Class="Test.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="test" Height="655" Width="610" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" WindowStartupLocation="CenterScreen" WindowStyle="SingleBorderWindow" Loaded="Window_Loaded">
    <Grid>			
    	<ListView Name="myList" VerticalAlignment="Stretch" IsSynchronizedWithCurrentItem="True" Width="600">
    		<ListView.View>
    			<GridView ColumnHeaderToolTip="test">
    				<GridViewColumn Width = "200" Header="firstname" />
					<GridViewColumn Width = "200" Header="lastname" />
					<GridViewColumn Width = "200" Header="city" />					
    			</GridView>			
    		</ListView.View>
				
    	</ListView></Grid>
</Window>

Open in new window

MSFanboyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

unmeshdaveCommented:
check the code. u need to provide displaymemberbinding to tell column, which member of data it has to display. moreover, you need to create dataview to set datacontext of listview.
//in your .cs replace Window_Loaded with following
       private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DataTable dt = new DataTable();
 
            dt.Columns.Add("first");
            dt.Columns.Add("last");
            dt.Columns.Add("city");
        
            DataView dv = new DataView(dt);
            myList.DataContext = dv;
            
            Binding bind = new Binding();
            myList.SetBinding(ListView.ItemsSourceProperty, bind);
 
            dt.Rows.Add("bill", "gates", "silicon valley");          
          
        }
 
//XAML replace listview element with following
 
       <ListView Name="myList" VerticalAlignment="Stretch" IsSynchronizedWithCurrentItem="True" Width="600">
            <ListView.View>
                <GridView ColumnHeaderToolTip="test">
                    <GridViewColumn Width = "200" Header="firstname" DisplayMemberBinding="{Binding Path=first}" />
                    <GridViewColumn Width = "200" Header="lastname" DisplayMemberBinding="{Binding Path=last}" />
                    <GridViewColumn Width = "200" Header="city" DisplayMemberBinding="{Binding Path=city}" />
                </GridView>
            </ListView.View>
 
        </ListView>

Open in new window

1
MSFanboyAuthor Commented:
check the code. u need to provide displaymemberbinding to tell column, which member of data it has to display. moreover, you need to create dataview to set datacontext of listview.

with the first I agree, but why should I create a dataview ? 
see this thread I dont use a dataview but the code works!

http://www.experts-exchange.com/Microsoft/Development/Microsoft_Programming/WPF_and_Silverlight/Q_24446234.html
0
unmeshdaveCommented:
well in that question, you didnot show how u r binding your data to listview. I have commented in that question please check.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

MSFanboyAuthor Commented:
why does your code have a dataview? A dataview in winforms I would have
choosen only when I want to filter stuff.
0
unmeshdaveCommented:
yes thats correct
I code with dataview as I thought normally we need filter/sorting kind of stuff when we work with databounded listview. thats the reason I have put dataview. Moreover, when you workiwith Gridview/Listview & bind with datatable there is a internal dataview is created by WPF. so there is no performance criteria exists.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MSFanboyAuthor Commented:
Now the DataGrid is out I really prefer it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.