Putting information from one window to a datagrid in another window without the use of database

Viewed 34

So my MainWindow with a datagrid is suppose to have an "add client" button and it will bring you to a new window and that window will ask information about the soon to be client and if I click the "save" button it should close the window and put the information to the datagrid on my MainWindow and I tried many things but I can't seem to get it right and I'm using wpf

public partial class GetInfo : Window
{


    public GetInfo()
    {
        InitializeComponent();
    }
    public class Client
    {
        public string name { get; set; }

        public string address { get; set; }

    }

    private void SaveBT_Click(object sender, System.EventArgs e)
    {

        MainWindow main = new MainWindow();

        Client addClient = new Client();
        addClient.name = NameTB.Text;
        addClient.address = AddressTB.Text;
        main.DataGridXAML.Items.Add(addClient);
        this.Close();



    }
}




public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
    }

    private void LogOut_Click(object sender, RoutedEventArgs e)
    {
        LoginScreen logout = new LoginScreen();
        logout.Show();
        Close();
    }

    private void AddClient_Click(object sender, RoutedEventArgs e)
    {
        GetInfo infoget = new GetInfo();
        infoget.Show();
    }

}
1 Answers

Seems like your GetInfo is working like a dialogue.

You may store the info in GetInfo and get info after user choose closed.

In your MainWindow

private void AddClient_Click(object sender, RoutedEventArgs e)
{
    GetInfo infoget = new GetInfo();
    infoget.ShowDialog();
    if( get.DialogResult != true ) {
        return;
    }
    main.DataGridXAML.Items.Add(addClient);
}

In GetInfo

public partial class GetInfo : Window
{
    public GetInfo()
    {
        InitializeComponent();
    }
    public class Client
    {
        public string name { get; set; }

        public string address { get; set; }
    }

    // Save info in window as a property.
    public Client Info { get; set; }
    private void SaveBT_Click(object sender, System.EventArgs e)
    {
        Client addClient = new Client();
        addClient.name = NameTB.Text;
        addClient.address = AddressTB.Text;
        Info = addClient;
        DialogueResult = true;
    }
}

What's wrong with your original code? Look at comments below.

private void SaveBT_Click(object sender, System.EventArgs e)
{
    // This is the problem, this main is NOT your actual instance of MainWindow which calls this GetInfo.
    MainWindow main = new MainWindow();

    Client addClient = new Client();
    addClient.name = NameTB.Text;
    addClient.address = AddressTB.Text;
    main.DataGridXAML.Items.Add(addClient);
    this.Close();
    // After this command, main is not getting used anymore, so you get nothing in your actual MainWindow
}
Related