ListView Adding data to column
- I'm using C# 2008 with .Net 3.5. I have a WPF with a tab control and inside the tab control I have list view with 3 columns. I'm trying to figure out how to add data to the 3 columns dynamically. Sorry I'm new at this and asking this might be a very trivial question. I have the columns of Month, Contract and Meeting. So row each row I want to add a different value for each column. How can I go about that. There was no overload operator for the ListView.Items.Add function to take more than 1 object.
Thanks in advance.
Bob
Todas las respuestas
- Hi,
Create an entity with the three properties you want.Create new instance of this class with the values you want for each of the field and add it to the ListView Items.
See this link for a sample
Hope it helps
Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem Ok, I'm still having some trouble. Below are code snippets. I hope someone can help me here. Basically, my ListView control has 25 entries in it, but there is no data showing.
Here is my code for the ListView control
<TabItem Header="Outrights" Name="tabIOutright"> <Grid> <ListView Margin="6" Name="lstVOutrights"> <ListView.View> <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Outrights Information"> <GridViewColumn Header="Contract" DisplayMemberBinding="{Binding Path=Contract_Name}" Width="200"/> <GridViewColumn Header="Meeting" DisplayMemberBinding="{Binding Path=MeetingDt}" Width="100"/> <GridViewColumn Header="Product Type" DisplayMemberBinding="{Binding Path=Prod_Type}" Width="100"/> <GridViewColumn Header="Product" DisplayMemberBinding="{Binding Path=Product}" Width="100"/> <GridViewColumn Header="Year" DisplayMemberBinding="{Binding Path=Contract_Yr}" Width="100"/> <GridViewColumn Header="Display" DisplayMemberBinding="{Binding Path=Display_Name}" Width="100"/> <GridViewColumn Header="Expiration" DisplayMemberBinding="{Binding Path=ExpirationDt}" Width="100"/> <GridViewColumn Header="BP 25" DisplayMemberBinding="{Binding Path=BP25Val}" Width="100"/> <GridViewColumn Header="BP 25 Left" DisplayMemberBinding="{Binding Path=BP25ValRemain}" Width="100"/> </GridView> </ListView.View> </ListView> </Grid> </TabItem>
Here is my code for my class constructor
The variables are all declared as private and I have get and set routines built for each variable.public clsContractData(string Name, string Type, string Prod, string Yr, string dName, DateTime Mtg, DateTime Exp, double BP25, double BP25R) { Contract_Name = Name; Prod_Type = Type; Product = Prod; Contract_Yr = Yr; Display_Name = dName; MeetingDt = Mtg; ExpirationDt = Exp; BP25Val = BP25; BP25ValRemain = BP25R; }
Finally here is my code that I'm using to try to populate the ListView
contractData is an array of class objects of type clsContractData and the array is size of 25 which is correct.public void CreateColumns() { for (int z = 0; z < contractData.GetLength(0); z++) { clsContractData tempContract = new clsContractData(); tempContract = contractData[z]; lstVOutrights.Items.Add(tempContract); tempContract = null; } }
When I debug the code, tempContract is filled in with the correct variables.
As I said, my list view has 25 entries in it, I can highlight 25 rows in ListView when the app is running, but no data is showing up.
Thanks
Bob- Hi Bob,
I tried another solution for your problem, using DataTemplate for each contract object and an ObservableCollection instead of an Array.
Check it out, maybe it will help your case:
XAML:
<Window.Resources> <DataTemplate x:Key="contractTemplate" DataType="{x:Type local:clsContractData}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Path=Contract_Name}" Grid.Column="0" Margin="10"/> <TextBlock Text="{Binding Path=Prod_Type}" Grid.Column="1" Margin="10"/> </Grid> </DataTemplate> </Window.Resources> <Grid> <ListView Margin="6" Name="lstVOutrights" ItemsSource="{Binding}" ItemTemplate="{StaticResource contractTemplate}" HorizontalContentAlignment="Stretch"/> </Grid>
Code Behind:
public partial class Window1 : Window { ObservableCollection<clsContractData> mContacts; public Window1() { InitializeComponent(); mContacts = new ObservableCollection<clsContractData>(); lstVOutrights.DataContext = mContacts; CreateColumns(); } public void CreateColumns() { mContacts.Add(new clsContractData("Rachel", "Type1")); mContacts.Add(new clsContractData("Mike", "Type2")); mContacts.Add(new clsContractData("Roy", "Type3")); mContacts.Add(new clsContractData("Tom", "Type2")); } } public class clsContractData { public string Contract_Name { get; set; } public string Prod_Type { get; set; } public clsContractData(string Name, string Type) { Contract_Name = Name; Prod_Type = Type; } }
Thanks for the help. I've got a question.
1st, what should my local be in
{x:Type local:clsContractData}
because when I start to build the code it says local is an undeclared namespace
Sorry if this is trivial, I've been learning C# on my own so I don't have anyone really explaining some stuff to me.
Here is my xaml file for my form<Window x:Class="FOA.ContractData" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Contract Data" Height="750" Width="1200" Background="LightGray" WindowStartupLocation="CenterScreen" Name="wndContractData" Closing="wndContractData_Closing" Loaded="wndContractData_Loaded"> <Window.Resources> <DataTemplate x:Key="contractTemplate" DataType="{x:Type local:clsContractData}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition x:Name="Contract" /> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Path=Contract_Name}" Grid.Column="0" Margin="10"/> <TextBlock Text="{Binding Path=Prod_Type}" Grid.Column="1" Margin="10"/> </Grid> </DataTemplate> </Window.Resources> <Grid> <Button Height="25" Margin="572.5,0,555,12" Name="btnExit" VerticalAlignment="Bottom" Click="btnExit_Click">Exit</Button> <DockPanel Height="Auto" HorizontalAlignment="Stretch" Margin="12,12,0,0" Name="dockPanel1" VerticalAlignment="Top" Width="Auto"></DockPanel> <TabControl Height="650" Name="tabRisk" Width="1150" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top"> <TabItem Header="Outrights" Name="tabIOutright"> <Grid> <ListView Margin="6" Name="lstVOutrights" ItemsSource="{Binding}" ItemTemplate="{StaticResource contractTemplate}" HorizontalContentAlignment="Stretch"/> </Grid> </TabItem> <TabItem Header="Spreads" Name="tabISpread" /> </TabControl> </Grid> </Window>
Thanks again!
Bob- Hi,
Local would be the namespace where your type clsContractData resides.Include it as you have done for xmlns:x which comes by default
See this link for some information
Hope it helps
Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem - Hi again,
Try adding something like : xmlns:local="clr-namespace:<NAMESPACE >" to theroot Window section of your XAML.
PYM. - I got it working. I went a little different path though. I do have another question though. I'm pulling dates from a database, they are saved as just Date but when I put them into my class because the property is a DateTime variable, it adds the time to the date. Is there a way when attaching this to my listview I can remove the time? And how can I align the data in the BP 25 column to the right?
BP25Val is of type Double.
Following is the code I added to try to get BP25Val to align to the right but it's not happening.
Here's my code for inserting the data into the ListView and it's working correctly.
public void CreateColumns() { ListView lstVOutrights = new ListView(); GridView myGridView = new GridView(); myGridView.AllowsColumnReorder = true; myGridView.ColumnHeaderToolTip = "Contract Information"; GridViewColumn gvc1 = new GridViewColumn(); gvc1.DisplayMemberBinding = new Binding("Contract_Name"); gvc1.Header = "Contract"; gvc1.Width = 100; myGridView.Columns.Add(gvc1); GridViewColumn gvc2 = new GridViewColumn(); gvc2.DisplayMemberBinding = new Binding("ExpirationDt"); gvc2.Header = "Expiration Date"; gvc2.Width = 100; myGridView.Columns.Add(gvc2); GridViewColumn gvc3 = new GridViewColumn(); //gvc3.DisplayMemberBinding = new Binding("BP25Val"); gvc3.Header = "25 BP"; gvc3.Width = 100; gvc3.CellTemplate = (DataTemplate)this.FindResource("myDT"); myGridView.Columns.Add(gvc3); for (int z = 0; z < contractData.GetLength(0); z++) { lstVOutrights.Items.Add(new myContracts(contractData[z])); lstVOutrights.View = myGridView; } myStackPanel.Children.Add(lstVOutrights); } }
Here's the code for the XAML
<Window x:Class="FOA.ContractData" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Contract Data" Height="750" Width="1200" Background="LightGray" WindowStartupLocation="CenterScreen" Name="wndContractData" Closing="wndContractData_Closing" Loaded="wndContractData_Loaded"> <Window.Resources> <DataTemplate x:Key="myDT"> <TextBlock Text="{Binding BP25Val}" TextAlignment="Right" /> </DataTemplate> </Window.Resources> <Grid Name="grdData"> <Grid.RowDefinitions> <RowDefinition Height="670"/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> </Grid.ColumnDefinitions> <DockPanel Grid.Row="0" Grid.Column="0" Height="Auto" HorizontalAlignment="Stretch" Margin="12,12,0,0" Name="dockPanel1" VerticalAlignment="Top" Width="Auto"> <TabControl Height="650" Name="tabRisk" Width="1150" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" DockPanel.Dock="Top"> <TabItem Header="Outrights" Name="tabIOutright"> <StackPanel Name="myStackPanel" HorizontalAlignment="Stretch"> </StackPanel> </TabItem> <TabItem Header="Spreads" Name="tabISpread" /> </TabControl> </DockPanel> <Button Grid.Row="1" Height="25" Margin="572.5,0,555,12" Name="btnExit" VerticalAlignment="Bottom" Click="btnExit_Click">Exit</Button> </Grid> </Window>
Thanks
Bob

