Answered by:
how to add data to colomn in Listview

Question
-
Hi everybody,
I need to add data to column of a listview in VB.net
imagine i need to add text in "textbox1" to column 1 in listview1 and add text in "textbox2" to column 2 in listview1.
how do i do that????
thanks!
- Edited by Supun Lakshan Sunday, January 29, 2012 5:32 AM
Sunday, January 29, 2012 5:29 AM
Answers
-
Hi,
Your datasource that gets bound would ideally have the data as you want.So the first item in the datasource would have only the first column property value and the second one would have only second column value.
What is the difficulty that you are facing here.If you can put up a sample of what you are trying,then we could help you more
Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem- Edited by Rahul P Nath Sunday, January 29, 2012 7:38 AM
- Marked as answer by Annabella Luo Thursday, February 9, 2012 9:45 AM
Sunday, January 29, 2012 7:35 AM -
Hi Supun,
Thank you for your post.
According to your description, I understand you want to add data to ListView.
Two situations, first, you want to add data in other two Textboxes, then use a button to submit. Fot this kind of situation, you can add the new data directly to the binding source as what Rahul has mentioned.
Second, you want to add row directly in ListView's last row, to achieve this you can refer to below complete sample:
http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-3-in-place-edit
If I misunderstand you or you have any additional questions, please feel free to let me know.
Have a nice day.
Annabella Luo[MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by Annabella Luo Thursday, February 9, 2012 9:45 AM
Monday, January 30, 2012 7:51 AM
All replies
-
Hi,
Your datasource that gets bound would ideally have the data as you want.So the first item in the datasource would have only the first column property value and the second one would have only second column value.
What is the difficulty that you are facing here.If you can put up a sample of what you are trying,then we could help you more
Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem- Edited by Rahul P Nath Sunday, January 29, 2012 7:38 AM
- Marked as answer by Annabella Luo Thursday, February 9, 2012 9:45 AM
Sunday, January 29, 2012 7:35 AM -
Hi,
Your data source bound to listview must be having multiple items. Any one item from the data source may be bound to textbox using 'Two Way' binding.
Two way binding with text box will result in changing the data source with the changes in textbox values. As soon as datasource will be updated, listview will be updated also to show the changed values.
For example, say PersonCollection is your datasource.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ListViewDemo { public class PersonCollection : List<Person> { // Default ctor is required by the WPF Designer. public PersonCollection() { } } public class Person { public Guid PersonID { get; set; } public string Name { get; set; } public int Age { get; set; } public DateTime DateOfBirth { get; set; } } }
And here is the xaml having listview and textboxes
<Window x:Class="ListViewDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ListViewDemo" Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="350" d:DesignWidth="636" SizeToContent="WidthAndHeight"> <Grid> <ListView Height="196" HorizontalAlignment="Left" Margin="33,12,0,0" Name="listView1" VerticalAlignment="Top" Width="541" ItemsSource="{Binding}" SelectionChanged="listView1_SelectionChanged"> <ListView.View> <GridView> <GridViewColumn Header="Person ID" Width="Auto" DisplayMemberBinding="{Binding Path=PersonID}" /> <GridViewColumn Header="Person Name" Width="Auto" DisplayMemberBinding="{Binding Path=Name}" /> <GridViewColumn Header="Age" Width="Auto" DisplayMemberBinding="{Binding Path=Age}" /> <GridViewColumn Header="DoB" Width="Auto" DisplayMemberBinding="{Binding Path=DateOfBirth}" /> </GridView> </ListView.View> </ListView> <Grid x:Name="EditAreaContainer"> <TextBox Height="23" HorizontalAlignment="Left" Margin="117,227,0,0" Name="textBox1" VerticalAlignment="Top" Width="211" Text="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="117,262,0,0" Name="textBox2" VerticalAlignment="Top" Width="58" Text="{Binding Path=Age, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> <Label Content="Name" Height="28" HorizontalAlignment="Left" Margin="33,225,0,0" Name="label1" VerticalAlignment="Top" Width="99" /> <Label Content="Age" Height="28" HorizontalAlignment="Left" Margin="33,260,0,0" Name="label2" VerticalAlignment="Top" Width="99" /> </Grid> </Grid> </Window>
Code with simple databinding
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; namespace ListViewDemo { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); SetInitBindings(); } private void SetInitBindings() { PersonCollection pList = GetData(); this.DataContext = pList; } /// <summary> /// Placeholder method for getting data /// </summary> /// <returns></returns> private static PersonCollection GetData() { //Generate sample data PersonCollection pList = new PersonCollection { new Person{ Name="Person1 name", Age=23, PersonID=Guid.NewGuid(), DateOfBirth=new DateTime(1982,2,20,15,3,43) }, new Person{ Name="Person2 name", Age=54, PersonID=Guid.NewGuid() , DateOfBirth=new DateTime(1959,3,17,5,58,51)}, new Person{ Name="Person3 name", Age=33, PersonID=Guid.NewGuid(), DateOfBirth=new DateTime(2008,7,29,8,24,39) }, new Person{ Name="Person4 name", Age=12, PersonID=Guid.NewGuid(), DateOfBirth=new DateTime(1997,8,12,19,53,24) }, }; return pList; } private void listView1_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (listView1.SelectedItem != null) { EditAreaContainer.DataContext = listView1.SelectedItem; } } } }
This will result in displaying the data in listview and textbox and on change of textbox data, it will also change listview.
Regards,
Vikas
Sunday, January 29, 2012 12:27 PM -
Hi Supun,
Thank you for your post.
According to your description, I understand you want to add data to ListView.
Two situations, first, you want to add data in other two Textboxes, then use a button to submit. Fot this kind of situation, you can add the new data directly to the binding source as what Rahul has mentioned.
Second, you want to add row directly in ListView's last row, to achieve this you can refer to below complete sample:
http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-3-in-place-edit
If I misunderstand you or you have any additional questions, please feel free to let me know.
Have a nice day.
Annabella Luo[MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by Annabella Luo Thursday, February 9, 2012 9:45 AM
Monday, January 30, 2012 7:51 AM -
Thursday, February 9, 2012 9:45 AM