我有一个wpf MainWindow.xaml。 MainWindow包含一个导航页面Type.xaml。导航页面有dataGrid和弹出窗口AddType.xaml。 AddType窗口将数据添加到数据库。 “类型”页面显示数据网格中的数据。我希望在关闭弹出窗口后刷新MainWindow(其中包含带有dataGrid的Type页面)。
<Grid>
<StackPanel>
<Menu Name="menu1" >
<MenuItem Header="Expenses" x:Name="typeExpense" Click="typeExpense_Click"/>
</Menu>
</StackPanel>
<Frame Name="Main" NavigationUIVisibility="Hidden" Margin="0,35,0,0">
</Frame>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void typeExpense_Click(object sender, RoutedEventArgs e)
{
Type type = new Type();
Main.NavigationService.Navigate(type);
}
}
<Grid>
<Button x:Name="btnAdd" Content="Dobavit" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="50,45,0,0" Click="btnAddWindow_Click"/>
<DataGrid x:Name="dataGrid" AutoGenerateColumns="False" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,100,10,10">
<DataGrid.Columns>
<DataGridTextColumn Header="Naimenovaniya" Binding="{Binding Path=typename}" Width="300"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
public partial class Type : Page
{
string connection = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
public Type()
{
InitializeComponent();
DataGridBind();
}
protected void DataGridBind()
{
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * from type", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGrid.ItemsSource = dt.DefaultView;
}
private void btnAddWindow_Click(object sender, RoutedEventArgs e)
{
AddType addtype = new AddType();
addtype.Show();
}
}
<Grid>
<Label x:Name="label" Content="Naimenovaniya" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="35,0,0,0"/>
<TextBox x:Name="txtType" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="364" Margin="45,26,0,0"/>
<Button x:Name="btnOK" Content="OK" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="85,77,0,0" Click="btnOK_Click"/>
<Button x:Name="btnCancel" Content="Otmena" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="219,77,0,0" Click="btnCancel_Click"/>
</Grid>
public partial class AddType : Window
{
string connection = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
public AddType()
{
InitializeComponent();
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO type (typename) VALUES ('"+txtType.Text+"')", con);
cmd.ExecuteNonQuery();
con.Close();
Close();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
答案 0 :(得分:-1)
由于您要在类型表中插入新记录,因此需要再次查询数据库。您可以通过处理窗口的private void initRealm() {
Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder()
.name("books.realm")
.schemaVersion(1)
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(config);
}
事件来执行此操作:
Close