abdujalil  chuliev

abdujalil chuliev

  • NA
  • 400
  • 41.2k

Refresh mainWindow Datagrid after closing popup childWindow

Aug 23 2017 2:51 AM
I have a wpf MainWindow.xaml. The MainWindow includes a navigation page Type.xaml. And the navigation page has dataGrid and a popup Window AddType.xaml. Now I want the MainWindow to be refreshed after I close the popup Window. Below is my code:
 
 MainWindow.xaml
 
  1. <Window x:Class="MyAccount.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6.         xmlns:local="clr-namespace:MyAccount"  
  7.         mc:Ignorable="d"  
  8.         Title="My personal budget" Height="700" Width="1200" WindowStartupLocation="CenterScreen">  
  9.     <Grid>  
  10.         <StackPanel>  
  11.             <Menu Name="menu1" >  
  12.                 <MenuItem Header="Expenses" x:Name="typeExpense" Click="typeExpense_Click"/>              
  13.             </Menu>  
  14.            </StackPanel>  
  15.         <Frame Name="Main" NavigationUIVisibility="Hidden" Margin="0,35,0,0">              
  16.         </Frame>  
  17.     </Grid>  
  18. </Window>  
 MainWindow CodeBehind:
  1. using System.Windows;  
  2.   
  3. namespace MyAccount  
  4. {  
  5.     public partial class MainWindow : Window  
  6.     {          
  7.         public MainWindow()  
  8.         {  
  9.             InitializeComponent();  
  10.         }  
  11.         private void typeExpense_Click(object sender, RoutedEventArgs e)  
  12.         {  
  13.             Type type = new Type();  
  14.             Main.NavigationService.Navigate(type);              
  15.         }  
  16.     }  
  17. }  
Type.xaml:
  1. <Page x:Class="MyAccount.Type"  
  2.       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
  5.       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
  6.       xmlns:local="clr-namespace:MyAccount"  
  7.       mc:Ignorable="d"   
  8.       d:DesignHeight="650" d:DesignWidth="1150"  
  9.       Title="Type">  
  10.     <Grid>  
  11.         <Button x:Name="btnAdd" Content="Dobavit" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="50,45,0,0" Click="btnAddWindow_Click"/>  
  12.         <DataGrid x:Name="dataGrid"  AutoGenerateColumns="False" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,100,10,10">  
  13.             <DataGrid.Columns>  
  14.                 <DataGridTextColumn Header="Naimenovaniya" Binding="{Binding Path=typename}"  Width="300"/>               
  15.             </DataGrid.Columns>  
  16.         </DataGrid>  
  17.     </Grid>  
  18. </Page>  
Type Code Behind:
  1. using System.Windows;  
  2. using System.Windows.Controls;  
  3. using System.Configuration;  
  4. using System.Data.SqlClient;  
  5. using System.Data;  
  6.   
  7. namespace MyAccount  
  8. {  
  9.     /// <summary>  
  10.     /// Interaction logic for Type.xaml  
  11.     /// </summary>  
  12.     public partial class Type : Page  
  13.     {  
  14.         string connection = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;  
  15.         public Type()  
  16.         {  
  17.             InitializeComponent();  
  18.             DataGridBind();  
  19.         }  
  20.         protected void DataGridBind()  
  21.         {  
  22.             SqlConnection con = new SqlConnection(connection);  
  23.             con.Open();  
  24.             SqlCommand cmd = new SqlCommand("SELECT * from type", con);  
  25.             SqlDataAdapter da = new SqlDataAdapter(cmd);  
  26.             DataTable dt = new DataTable();  
  27.             da.Fill(dt);  
  28.             dataGrid.ItemsSource = dt.DefaultView;  
  29.         }  
  30.   
  31.         private void btnAddWindow_Click(object sender, RoutedEventArgs e)  
  32.         {  
  33.             AddType addtype = new AddType();  
  34.             addtype.Show();  
  35.         }  
  36.     }  
  37. }  
AddType.xaml:
  1. <Window x:Class="MyAccount.AddType"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6.         xmlns:local="clr-namespace:MyAccount"  
  7.         mc:Ignorable="d"  
  8.         Title="Dobavit kategorii" Height="200" Width="500">  
  9.     <Grid>  
  10.         <Label x:Name="label" Content="Naimenovaniya" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="35,0,0,0"/>  
  11.         <TextBox x:Name="txtType" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="364" Margin="45,26,0,0"/>  
  12.         <Button x:Name="btnOK" Content="OK" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="85,77,0,0" Click="btnOK_Click"/>  
  13.         <Button x:Name="btnCancel" Content="Otmena" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="219,77,0,0" Click="btnCancel_Click"/>  
  14.   
  15.     </Grid>  
  16. </Window>  
AddType CodeBehind
 
  1. using System.Configuration;  
  2. using System.Data.SqlClient;  
  3. using System.Windows;  
  4.   
  5. namespace MyAccount  
  6. {  
  7.     public partial class AddType : Window  
  8.     {  
  9.         string connection = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;  
  10.         public AddType()  
  11.         {  
  12.             InitializeComponent();  
  13.         }  
  14.   
  15.         private void btnOK_Click(object sender, RoutedEventArgs e)  
  16.         {  
  17.             SqlConnection con = new SqlConnection(connection);  
  18.             con.Open();  
  19.             SqlCommand cmd = new SqlCommand("INSERT INTO type (typename) VALUES ('"+txtType.Text+"')", con);  
  20.             cmd.ExecuteNonQuery();  
  21.             con.Close();  
  22.             Close();  
  23.         }  
  24.   
  25.         private void btnCancel_Click(object sender, RoutedEventArgs e)  
  26.         {  
  27.             Close();  
  28.         }  
  29.   
  30.     }  
  31.  

Attachment: MyAccount.rar

Answers (1)