locked
Display DataGrid record value from one form in ComboBox in another form RRS feed

  • Question

  • Hi All.

    I have one form with DataGrid where each record row has button that open another form to edit the according record. Both form link to EF table. When I click the button in DataGrid the TextBoxes are display record values from DataGrid but ComboBoxes does not. How to display value in ComboBox? I will appreciate for sample and explanation.

    Thanks.







    • Edited by zleug Wednesday, May 27, 2020 4:21 AM
    • Moved by CoolDadTx Wednesday, May 27, 2020 1:35 PM WPF related
    Wednesday, May 27, 2020 12:37 AM

All replies

  • Hi zleug,

    Thank you for posting here.

    This seems to be a WPF issue, right?

    If so, please ask your question at Microsoft Q&A for better support.

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Timon


    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.

    Wednesday, May 27, 2020 6:04 AM
  • Hi Timon Yang.

    My project is WPF. But my post about C# programming with EF.

    Thanks.
    Wednesday, May 27, 2020 12:26 PM
  • Your question is related to data binding in WPF. Please post your question in the new WPF forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, May 27, 2020 1:34 PM
  • Hello,

    The base for this is shown here.

    I took the code and created a demo which stops at getting the current row and presenting data in a MessageBox rather than a new window, for you simply pass the object via a window constructor.

    Look at this class as from EF with INotifyPropertyChanged implemented.

    using System;
    using System.ComponentModel;
    
    namespace WpfApp1
    {
        class DemoModel : INotifyPropertyChanged
        {
            protected string _text;
            public string Text
            {
                get => _text;
                set
                {
                    _text = value; RaisePropertyChanged("Text");
                }
            }
    
            protected string _dynamicText;
            public string DynamicText
            {
                get => _dynamicText;
                set
                {
                    _dynamicText = value;
                    RaisePropertyChanged("DynamicText");
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            public void RaisePropertyChanged(String propertyName)
            {
                PropertyChanged?.Invoke(this, 
                    new PropertyChangedEventArgs(propertyName));
            }
    
            public override string ToString() => Text;
    
        }
    }

    Code behind

    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Windows.Controls;
    
    namespace WpfApp1
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
    
                var models = new ObservableCollection<DemoModel>
                {
                    new DemoModel() {Text = "Some Text #1."},
                    new DemoModel() {Text = "Some Text #2."},
                    new DemoModel() {Text = "Some Text #3."},
                    new DemoModel() {Text = "Some Text #4."},
                    new DemoModel() {Text = "Some Text #5."}
                };
    
                DataGrid1.ItemsSource = models;
    
            }
    
            private void ChangeText(object sender, RoutedEventArgs e)
            {
                var model = (DemoModel) (sender as Button)?.DataContext;
                MessageBox.Show($"{model.Text}\n{model.DynamicText}", "Test", 
                    MessageBoxButton.OK,
                    MessageBoxImage.Exclamation);
    
            }
        }
    }
    

    XAML

    <Window x:Class="WpfApp1.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="Window1" Height="241.77" Width="250.298">
        <Grid>
            <DataGrid Name="DataGrid1">
                <DataGrid.Columns>
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Width="55" Click="ChangeText">Get</Button>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, May 27, 2020 1:46 PM
  • Hi Karen. Thanks for reply.

    Like I posted I don't have a problem to display DataGrid record value in TextFields of the child form. My problem to display according some value from DataGrid record row in ComboBox of the child form. 

    Thanks.



    • Edited by zleug Wednesday, May 27, 2020 2:07 PM
    Wednesday, May 27, 2020 2:01 PM
  • Hi Karen. Thanks for reply.

    Like I posted I don't have a problem to display DataGrid record value in TextFields of the child form. My problem to display according some value from DataGrid record row in ComboBox of the child form. 

    Thanks.



    As already stated you need to post in the Microsoft Q&A forum then.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, May 27, 2020 2:30 PM