Click here to Skip to main content
16,012,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Comments
Thanks7872 25-Jul-13 4:58am    
Dont dump the codes here. Post only the code you have problem with.

1 solution

u need get the selected index firstly .

C#
if (listView1.SelectedItems.Count == 1)
{
    string id = listView1.SelectedItems[0].Text;
}
 
Share this answer
 
Comments
prapaug 25-Jul-13 4:24am    
its not working...i wanna bind the entire list to datagrid first and then display contents according to my selection.
Chui PuiKwan 25-Jul-13 5:02am    
http://msdn.microsoft.com/en-us/library/fkx0cy6d(v=vs.80).aspx

your main problem is that you must bind you datasource to the gridview .
prapaug 25-Jul-13 5:24am    
how can i bind my list to datagrid view..
please give me the code.
Chui PuiKwan 25-Jul-13 6:54am    
sorry.I got out and cook now.
now.i come back and give you some codes,as follows:


DataTable dt = new DataTable();
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
DataRow dr = dt.NewRow();
dr["Column1"] = "123456";
dr["Column2"] = "654321";
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900