i have an xaml code like one below:
<window x:class="WpfApplication20.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication20"
Title="MainWindow" Height="350" Width="525">
<grid>
<listview height="82" horizontalalignment="Left" name="listView1" verticalalignment="Top" width="120"> <listviewitem content="STUDENT 1" fontsize="16" fontweight="Bold" selected="ListViewItem_Selected"> <listviewitem content="STUDENT 2" fontsize="16" fontweight="Bold" selected="ListViewItem_Selected_1"> <listviewitem content="STUDENT 3" fontsize="16" fontweight="Bold" selected="ListViewItem_Selected_2"> <listviewitem content="STUDENT 4" fontsize="16" fontweight="Bold" selected="ListViewItem_Selected_3"> <datagrid autogeneratecolumns="True" height="311" horizontalalignment="Left" margin="281,0,0,0" name="dataGrid1" verticalalignment="Top" width="222" itemssource="{Binding personaldetails}">
<datagrid.columns>
<datagridtextcolumn header="Name" width="75" canuserresize="False" binding="{Binding Name}" canuserreorder="False" isreadonly="True">
<datagridtextcolumn header="Id" width="75" canuserresize="False" binding="{Binding Id}" canuserreorder="False" isreadonly="True">
<datagridtextcolumn header="Address" width="75" canuserresize="False" binding="{Binding Address}" canuserreorder="False" isreadonly="True">
and my code behind is:
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.IO;
using System.Data;
using System.Xml.Serialization;
namespace WpfApplication20
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public class student
{
public string name;
public string id;
public string address;
}
List<student> personaldetails = new List<student>();
public MainWindow()
{
InitializeComponent();
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Id");
dt.Columns.Add("Address");
dataGrid1.ItemsSource = dt.DefaultView;
student stdnt1 = new student();
stdnt1.name = "qqq";
stdnt1.id = "qqq";
stdnt1.address = "qqq";
personaldetails.Add(stdnt1);
student stdnt2 = new student();
stdnt2.name = "www";
stdnt2.id = "www";
stdnt2.address = "www";
personaldetails.Add(stdnt2);
student stdnt3 = new student();
stdnt3.name = "eee";
stdnt3.id = "eee";
stdnt3.address = "eee";
personaldetails.Add(stdnt3);
student stdnt4 = new student();
stdnt4.name = "rrr";
stdnt4.id = "rrr";
stdnt4.address = "rrr";
personaldetails.Add(stdnt4);
}
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void ListViewItem_Selected(object sender, RoutedEventArgs e)
{
DataView dv = dataGrid1.ItemsSource as DataView;
DataTable dt = dv.Table;
DataRow dr = dt.NewRow();
dr["Name"] = personaldetails[0].name;
dr["Id"] = personaldetails[0].id;
dr["Address"] = personaldetails[0].address;
dt.Rows.Add(dr);
}
private void ListViewItem_Selected_1(object sender, RoutedEventArgs e)
{
DataView dv = dataGrid1.ItemsSource as DataView;
DataTable dt = dv.Table;
DataRow dr = dt.NewRow();
dr["Name"] = personaldetails[1].name;
dr["Id"] = personaldetails[1].id;
dr["Address"] = personaldetails[1].address;
dt.Rows.Add(dr);
}
private void ListViewItem_Selected_2(object sender, RoutedEventArgs e)
{
DataView dv = dataGrid1.ItemsSource as DataView;
DataTable dt = dv.Table;
DataRow dr = dt.NewRow();
dr["Name"] = personaldetails[2].name;
dr["Id"] = personaldetails[2].id;
dr["Address"] = personaldetails[2].address;
dt.Rows.Add(dr);
}
private void ListViewItem_Selected_3(object sender, RoutedEventArgs e)
{
DataView dv = dataGrid1.ItemsSource as DataView;
DataTable dt = dv.Table;
DataRow dr = dt.NewRow();
dr["Name"] = personaldetails[3].name;
dr["Id"] = personaldetails[3].id;
dr["Address"] = personaldetails[3].address;
dt.Rows.Add(dr);
}
}
}
what i want is that i want to bind the entire list of contents to datagrid and from that i wanna display the content according to my selection.
Please help me.