none
how to pass values from textbox to listview using click button in wpf? RRS feed

  • Question

  • I want know how to pass values from textbox to listview using click button in wpf  so please help me out from this soon
    Wednesday, November 8, 2017 7:14 PM

Answers

  • Hi,

        <Grid>
            <Canvas>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Item Id" VerticalAlignment="Top" Canvas.Left="33" Canvas.Top="45"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="ItemName" VerticalAlignment="Top" Canvas.Left="172" Canvas.Top="45"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Price" VerticalAlignment="Top" Canvas.Left="454" Canvas.Top="45"/>
                <TextBox x:Name="Itemnolist" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"  Canvas.Left="28" Canvas.Top="76"/>
                <ListView x:Name="listview1"  ItemsSource="{Binding itemlists}" 
                          HorizontalAlignment="Left" VerticalAlignment="Top" 
                          Width="684" Panel.ZIndex="1" Background="Transparent" 
                          Visibility="Visible" BorderThickness="0"                   
                          Height="400" MinHeight="0"
                          HorizontalContentAlignment="Center"                    
                          Canvas.Left="33" Canvas.Top="127">
                    <ListView.View>
                        <GridView >
                            <GridViewColumn Header="ItemNo" DisplayMemberBinding="{Binding ItemNo}" Width="113"/>
                            <GridViewColumn Header="ItemName" DisplayMemberBinding="{Binding ItemName}" Width="113"/>
                            <GridViewColumn Header="Quantity" DisplayMemberBinding="{Binding Quantity}" Width="113"/>
                            <GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price}" Width="113"/>
                            <GridViewColumn Header="Total" DisplayMemberBinding="{Binding Total}" Width="113"/>
                            <GridViewColumn Header="Edit/Delete" Width="115">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <Button x:Name="edit" Margin="2,0" Click="edit_Click" Tag="{Binding ItemNo}" Content="EDIT" />
                                            <Button x:Name="delete" Margin="2,0" Click="delete_Click" Tag="{Binding ItemNo}" Content="Delete" />
                                        </StackPanel>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                        </GridView>
                    </ListView.View>
                </ListView>
                <TextBox x:Name="Itemnamelist" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Canvas.Left="172" Canvas.Top="75"/>
                <TextBox x:Name="Pricelist" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"  Canvas.Left="454" Canvas.Top="74"/>
                <Button x:Name="Addbtn" Content="ADD" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Button_Click" Canvas.Left="732" Canvas.Top="74"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Quantity" VerticalAlignment="Top" Canvas.Left="313" Canvas.Top="45"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Total" VerticalAlignment="Top" Canvas.Left="597" Canvas.Top="45"/>
                <TextBox x:Name="Quantitylist" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"  Canvas.Left="313" Canvas.Top="75"/>
                <TextBox x:Name="Totallistlist"  HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="" Canvas.Left="597" Canvas.Top="74"/>
            </Canvas>
    
        </Grid>


      public partial class sample11 : Window
        {
            public sample11()
            {
                InitializeComponent();
                itemlists = new ObservableCollection<itemlist>();
                itemlists.Add(new itemlist { ItemNo = "1", ItemName = "item1", Price = "19", Quantity = "12", Total = "blabla..." });
                itemlists.Add(new itemlist { ItemNo = "2", ItemName = "item2", Price = "20", Quantity = "44", Total = "blabla..." });
                itemlists.Add(new itemlist { ItemNo = "3", ItemName = "item3", Price = "21", Quantity = "31", Total = "blabla..." });
                this.DataContext = this;
            }
    
            private ObservableCollection<itemlist> _itemlist;
            public ObservableCollection<itemlist> itemlists
            {
                get { return _itemlist; }
                set { _itemlist = value; }
            }
    
    
            public class itemlist
            {
                public string ItemNo { get; set; }
                public string ItemName { get; set; }
                public string Quantity { get; set; }
                public string Price { get; set; }
                public string Total { get; set; }
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                itemlists.Add(new itemlist()
                {
                    ItemNo = Itemnolist.Text.ToString(),
                    ItemName = Itemnamelist.Text.ToString(),
                    Quantity = Quantitylist.Text.ToString(),
                    Price = Pricelist.Text.ToString(),
                    Total = Totallistlist.Text.ToString()
                });
      
                Clearall();
    
            }
            private void Clearall()
            {
                Itemnolist.Clear();
                Itemnamelist.Clear();
                Quantitylist.Clear();
                Pricelist.Clear();
                Totallistlist.Clear();
            }
    
            private void edit_Click(object sender, RoutedEventArgs e)
            {
                //here you can show a edit form
            }
    
            private void delete_Click(object sender, RoutedEventArgs e)
            {
                Button btn = sender as Button;
                itemlists.Remove(itemlists.Single(s => s.ItemNo == btn.Tag.ToString()));                  
            }
        }

    Sincerely,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 9, 2017 7:15 AM
    Moderator

All replies

  • Are you using databinding?

    Do you want to add the value in the TextBox to the list of items in the ListView?

    You will get better answers quickly if you provide enough details for people to help.


    Lloyd Sheen

    Wednesday, November 8, 2017 7:32 PM
  • i am using two table one is purchase another is sales so bring purchase table to sales table which have prodid to textbox,name to textbox price to textbox so on I want to add values from textbox to listview then edit and delete dynamically after insert to sales table using click button wpf help me, i wrote that below is it good or not tell me

    Code behind

    public partial class MainWindow : Window
        {
            private ObservableCollection<itemlist> _itemlist;
            public ObservableCollection<itemlist> itemlists
            {
                get { return _itemlist; }
                set { _itemlist = value; }
            }
            public MainWindow()
            {
                InitializeComponent();
                itemlists = new ObservableCollection<itemlist>();
            }

            public class itemlist
            {
                public string ItemNo { get; set; }
                public string ItemName { get; set; }
                public string Quantity { get; set; }
                public string Price { get; set; }
                public string Total { get; set; }
            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                itemlists.Add(new itemlist()
                {
                    ItemNo = Itemnolist.Text.ToString(),
                    ItemName = Itemnamelist.Text.ToString(),
                    Quantity = Quantitylist.Text.ToString(),
                    Price = Pricelist.Text.ToString(),
                    Total = Totallistlist.Text.ToString()
                });
                this.listview1.ItemsSource = itemlists;
                Clearall();
               
            }
            private void Clearall()
            {
                Itemnolist.Clear();
                Itemnamelist.Clear();
                Quantitylist.Clear();
                Pricelist.Clear();
                Totallistlist.Clear();
            }

    <Grid>
            <Canvas>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Item Id" VerticalAlignment="Top" Canvas.Left="33" Canvas.Top="45"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="ItemName" VerticalAlignment="Top" Canvas.Left="172" Canvas.Top="45"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Price" VerticalAlignment="Top" Canvas.Left="454" Canvas.Top="45"/>
                <TextBox x:Name="Itemnolist" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" TextChanged="Itemno_TextChanged" Canvas.Left="28" Canvas.Top="76"/>
                <ListView x:Name="listview1" ItemsSource="{Binding}" 
                          HorizontalAlignment="Left" VerticalAlignment="Top" 
                          Width="684" Panel.ZIndex="1" Background="Transparent" 
                          Visibility="Visible" BorderThickness="0" 
                          SelectionChanged="listviewsource_SelectionChanged" 
                          Height="Auto" MinHeight="0"
                          HorizontalContentAlignment="Center"                    
                          Canvas.Left="33" Canvas.Top="127">
                    <ListView.View>
                        <GridView >
                            <GridViewColumn Header="ItemNo" DisplayMemberBinding="{Binding ItemNo}" Width="113"/>
                            <GridViewColumn Header="ItemName" DisplayMemberBinding="{Binding ItemName}" Width="113"/>
                            <GridViewColumn Header="Quantity" DisplayMemberBinding="{Binding Quantity}" Width="113"/>
                            <GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price}" Width="113"/>
                            <GridViewColumn Header="Total" DisplayMemberBinding="{Binding Total}" Width="113"/>
                            <GridViewColumn Header="Edit/Delete" Width="115">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Button x:Name="edit" Content="EDIT" Click="edit_Click"/>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                        </GridView>
                    </ListView.View>
                </ListView>
                <TextBox x:Name="Itemnamelist" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Canvas.Left="172" Canvas.Top="75"/>
                <TextBox x:Name="Pricelist" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" TextChanged="Price_TextChanged" Canvas.Left="454" Canvas.Top="74"/>
                <Button x:Name="Addbtn" Content="ADD" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Button_Click" Canvas.Left="732" Canvas.Top="74"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Quantity" VerticalAlignment="Top" Canvas.Left="313" Canvas.Top="45"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Total" VerticalAlignment="Top" Canvas.Left="597" Canvas.Top="45"/>
                <TextBox x:Name="Quantitylist" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="Quantity_TextChanged" Canvas.Left="313" Canvas.Top="75"/>
                <TextBox x:Name="Totallistlist"  HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="" Canvas.Left="597" Canvas.Top="74"/>
            </Canvas>

    </Grid>



    Wednesday, November 8, 2017 9:49 PM
  • Hi,

        <Grid>
            <Canvas>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Item Id" VerticalAlignment="Top" Canvas.Left="33" Canvas.Top="45"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="ItemName" VerticalAlignment="Top" Canvas.Left="172" Canvas.Top="45"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Price" VerticalAlignment="Top" Canvas.Left="454" Canvas.Top="45"/>
                <TextBox x:Name="Itemnolist" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"  Canvas.Left="28" Canvas.Top="76"/>
                <ListView x:Name="listview1"  ItemsSource="{Binding itemlists}" 
                          HorizontalAlignment="Left" VerticalAlignment="Top" 
                          Width="684" Panel.ZIndex="1" Background="Transparent" 
                          Visibility="Visible" BorderThickness="0"                   
                          Height="400" MinHeight="0"
                          HorizontalContentAlignment="Center"                    
                          Canvas.Left="33" Canvas.Top="127">
                    <ListView.View>
                        <GridView >
                            <GridViewColumn Header="ItemNo" DisplayMemberBinding="{Binding ItemNo}" Width="113"/>
                            <GridViewColumn Header="ItemName" DisplayMemberBinding="{Binding ItemName}" Width="113"/>
                            <GridViewColumn Header="Quantity" DisplayMemberBinding="{Binding Quantity}" Width="113"/>
                            <GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price}" Width="113"/>
                            <GridViewColumn Header="Total" DisplayMemberBinding="{Binding Total}" Width="113"/>
                            <GridViewColumn Header="Edit/Delete" Width="115">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <Button x:Name="edit" Margin="2,0" Click="edit_Click" Tag="{Binding ItemNo}" Content="EDIT" />
                                            <Button x:Name="delete" Margin="2,0" Click="delete_Click" Tag="{Binding ItemNo}" Content="Delete" />
                                        </StackPanel>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                        </GridView>
                    </ListView.View>
                </ListView>
                <TextBox x:Name="Itemnamelist" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Canvas.Left="172" Canvas.Top="75"/>
                <TextBox x:Name="Pricelist" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"  Canvas.Left="454" Canvas.Top="74"/>
                <Button x:Name="Addbtn" Content="ADD" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Button_Click" Canvas.Left="732" Canvas.Top="74"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Quantity" VerticalAlignment="Top" Canvas.Left="313" Canvas.Top="45"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Total" VerticalAlignment="Top" Canvas.Left="597" Canvas.Top="45"/>
                <TextBox x:Name="Quantitylist" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"  Canvas.Left="313" Canvas.Top="75"/>
                <TextBox x:Name="Totallistlist"  HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="" Canvas.Left="597" Canvas.Top="74"/>
            </Canvas>
    
        </Grid>


      public partial class sample11 : Window
        {
            public sample11()
            {
                InitializeComponent();
                itemlists = new ObservableCollection<itemlist>();
                itemlists.Add(new itemlist { ItemNo = "1", ItemName = "item1", Price = "19", Quantity = "12", Total = "blabla..." });
                itemlists.Add(new itemlist { ItemNo = "2", ItemName = "item2", Price = "20", Quantity = "44", Total = "blabla..." });
                itemlists.Add(new itemlist { ItemNo = "3", ItemName = "item3", Price = "21", Quantity = "31", Total = "blabla..." });
                this.DataContext = this;
            }
    
            private ObservableCollection<itemlist> _itemlist;
            public ObservableCollection<itemlist> itemlists
            {
                get { return _itemlist; }
                set { _itemlist = value; }
            }
    
    
            public class itemlist
            {
                public string ItemNo { get; set; }
                public string ItemName { get; set; }
                public string Quantity { get; set; }
                public string Price { get; set; }
                public string Total { get; set; }
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                itemlists.Add(new itemlist()
                {
                    ItemNo = Itemnolist.Text.ToString(),
                    ItemName = Itemnamelist.Text.ToString(),
                    Quantity = Quantitylist.Text.ToString(),
                    Price = Pricelist.Text.ToString(),
                    Total = Totallistlist.Text.ToString()
                });
      
                Clearall();
    
            }
            private void Clearall()
            {
                Itemnolist.Clear();
                Itemnamelist.Clear();
                Quantitylist.Clear();
                Pricelist.Clear();
                Totallistlist.Clear();
            }
    
            private void edit_Click(object sender, RoutedEventArgs e)
            {
                //here you can show a edit form
            }
    
            private void delete_Click(object sender, RoutedEventArgs e)
            {
                Button btn = sender as Button;
                itemlists.Remove(itemlists.Single(s => s.ItemNo == btn.Tag.ToString()));                  
            }
        }

    Sincerely,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 9, 2017 7:15 AM
    Moderator
  • thanks bob i did delete as you said and edit button also again thanks to you,now i am changing text value after to update to listview items 
    Friday, November 10, 2017 12:04 AM
  • can u you help me my problem is i could not check whether product number already exists or not in listview in wpf? i searched a lot of website but does not give right solutions for this so help me out from this
    Sunday, November 12, 2017 9:00 PM
  • You have to stop thinking of controls when you want to check your data.  You have data and you have a UI.  Check the items in the collection that is bound to the ItemsSource of the ListView.

    You have itemslists which is a collection of itemlist.  Simply check if the (and here I am somewhat confused) perhaps ItemNo is product number???

    If you want good answers please check your posts and try to match the code you show with the question you ask.  (and I won't even get into asking more than one question per post).


    Lloyd Sheen

    Sunday, November 12, 2017 9:37 PM
  • thanks sheen for your concern
    Thursday, November 30, 2017 8:58 PM