Search code examples
c#sqlwpfdatagrid

How to fix sending data from new instance of form to datagrid on Main Form


I had this simple inventory manager in WPF using a datagrid hooked up to a table using SQL working the other day but changed a line of the code and now it's broken. I've slept since I broke it and can't work out how to fix it again.

The problem seems to be with with dataGrid1.Items.Add(testtables) from the MainWindow class below.

I ran it in a trycatch with the exception message saying that the operation is not valid while ItemsSource is in use, and that ItemsControl.ItemsSource should be used to access and modify elements instead.

Here's the code for the main form

namespace WpfApp2
{

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        DataClasses1DataContext dc = new DataClasses1DataContext(Properties.Settings.Default.TestingConnectionString);

        public MainWindow()
        {
            InitializeComponent();
            if (dc.DatabaseExists())
            {
                dataGrid1.ItemsSource = dc.TestTables;
            }
        }

        private void newButton_Click(object sender, RoutedEventArgs e)
        {
            Window1 window1 = new Window1();
            window1.Show();
        }

        public void NewLine (int ID, string nm, decimal pc)
        {
            TestTable testtable = new TestTable
            {
                ID = ID,
                Name = nm,
                Price = pc
            };

            dataGrid1.Items.Add(testtable);

        }

        private void saveButton_Click_1(object sender, RoutedEventArgs e)
        {
            dc.SubmitChanges();
        }

    }
}

XAML code for MainWindow

<Window x:Class="WpfApp2.MainWindow"
        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:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button x:Name="newBtn" Content="New" HorizontalAlignment="Left" Margin="217,62,0,0" VerticalAlignment="Top" Width="75" Click="newButton_Click"/>
        <Button x:Name="save" Content="Save" HorizontalAlignment="Left" Margin="401,82,0,0" VerticalAlignment="Top" Width="75" Click="saveButton_Click_1"/>
        <DataGrid x:Name="dataGrid1" HorizontalAlignment="Left" Height="100" Margin="77,236,0,0" VerticalAlignment="Top" Width="575" IsReadOnly="True"/>

    </Grid>
</Window>

and here's the code for the secondary form that asks the user to input the data

public partial class Window1 : Window
    {
        public int iDNumber;
        public string name;
        public decimal price;

        public Window1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            iDNumber = Convert.ToInt32(iDTextBox.Text);
            name = Convert.ToString(nameTextBox.Text);
            price = Convert.ToDecimal(priceTextBox.Text);
            ((MainWindow)Application.Current.MainWindow).NewLine(iDNumber, name, price);
            this.Close();
        }
    }

XAML code for Window1

<Window x:Class="WpfApp2.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:WpfApp2"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="400">
    <Grid Margin="0,0,0,0">
        <Button Content="Save" HorizontalAlignment="Left" Margin="156,362,0,0" VerticalAlignment="Top" Width="76" Click="Button_Click"/>
        <TextBox x:Name="iDTextBox" HorizontalAlignment="Left" Height="23" Margin="156,79,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="nameTextBox" HorizontalAlignment="Left" Height="23" Margin="156,117,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="priceTextBox" HorizontalAlignment="Left" Height="23" Margin="156,155,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label Content="ID" HorizontalAlignment="Left" Margin="95,79,0,0" VerticalAlignment="Top"/>
        <Label Content="Name" HorizontalAlignment="Left" Margin="95,117,0,0" VerticalAlignment="Top"/>
        <Label Content="Price" HorizontalAlignment="Left" Margin="95,155,0,0" VerticalAlignment="Top"/>

    </Grid>
</Window>

When it was working the other day, it nicely added the new line of data to the SQL database (however didn't automatically show the updated info in the application, but this isn't the problem at the moment).


Solution

  • In this section of your code:

      public MainWindow()
        {
            InitializeComponent();
            if (dc.DatabaseExists())
            {
                dataGrid1.ItemsSource = dc.TestTables;
            }
        }
    

    You are displaying the data by assigning the datatable to the datagrid.ItemsSource property. If you do this, you need to add items by modifying the DataTable instead of the DataGrid.

    dataGrid1.Items.Add(testtable);
    

    So instead of what's above, try adding the testtable item to your existing collection dc.TestTables