在DataGrid中刷新数据库 [英] Refreshing Database in DataGrid

查看:79
本文介绍了在DataGrid中刷新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表table1和table2.当我单击button1时,DataGrid dg1将显示表1,类似地,当我单击button2时,DataGrid dg2将显示table2.即使将控件移交给其他事件(button1_click到Button2_click,反之亦然),我在这里仍然面临的问题是DataGrid的内容仍然保留并且新数据不断地被添加.我想每次都刷新DataGrid,以便清除旧数据.是否有类似"Refresh()","Clear()"之类的方法;

I have two tables table1 and table2. when I click button1 the DataGrid dg1 will show table 1 ,similarly when I click button2 DataGrid dg2 will show table2 . the problem which I face here even when control handover to other event (button1_click to Button2_click and vice versa)the content of the DataGrid remain and the new data repeatedly getting added . I want to referesh the DataGrid each time so that the old data will be cleared. is there any method there like "Refresh()" "Clear()" to do so;

private void button2_Click_1(object sender, RoutedEventArgs e)
       {
           connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Project/Book.accdb";
           connection = new OleDbConnection(connectionstring);
           sql = "Select*from AuthorISBN";




           dg2.Visibility = Visibility.Visible;
           dg1.Visibility = Visibility.Hidden;





           Thickness th = new Thickness();
           th.Bottom = 0;
           th.Top = 0;
           th.Left = 0;
           th.Right = 0;
           dg2.Margin = th;

           dg2.AutoGenerateColumns = true;

           try
           {
               connection.Open();
               oledbAdapter = new OleDbDataAdapter(sql, connection);
               oledbAdapter.Fill(dt);
               //dg1.DataContext = dt;
               dg2.ItemsSource = dt.DefaultView;
               dg2.CanUserAddRows = true;

               connection.Close();



           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }

           //dg1.AutoGeneratingColumn+=new EventHandler<DataGridAutoGeneratingColumnEventArgs>(dg1_AutoGeneratingColumn);
           dg2.HorizontalAlignment = HorizontalAlignment.Left;
           dg2.VerticalAlignment = VerticalAlignment.Top;
           dg2.Height = 700;
           dg2.Width = 900;
           dg2.Background = Brushes.Violet;
           s2.Children.Clear();
           s2.Children.Add(dg2);

       }

       private void button1_Click_1(object sender, RoutedEventArgs e)
       {
           connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Project/Book.accdb";
           connection = new OleDbConnection(connectionstring);
           sql = "Select*from Authors";

           dt.Dispose();


           dg2.Visibility = Visibility.Hidden;
           dg1.Visibility = Visibility.Visible;





           Thickness th = new Thickness();
           th.Bottom = 0;
           th.Top = 0;
           th.Left = 0;
           th.Right = 0;
           dg1.Margin = th;

           dg1.AutoGenerateColumns = true;

           try
           {
               connection.Open();
               oledbAdapter = new OleDbDataAdapter(sql, connection);
               oledbAdapter.Fill(dt);
               //dg1.DataContext = dt;

               dg1.ItemsSource = dt.DefaultView;
               dg1.CanUserAddRows = true;

               connection.Close();



           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }

           //dg1.AutoGeneratingColumn+=new EventHandler<DataGridAutoGeneratingColumnEventArgs>(dg1_AutoGeneratingColumn);
           dg1.HorizontalAlignment = HorizontalAlignment.Left;
           dg1.VerticalAlignment = VerticalAlignment.Top;
           dg1.Height = 700;
           dg1.Width = 900;
           dg1.Background = Brushes.Violet;
           s2.Children.Clear();
           s2.Children.Add(dg1);

       }





any help will be appreaciated

推荐答案

您好.为什么没有2个收藏夹?
我更喜欢将网格绑定到集合,此后,如果我修改集合,则网格将自动更改.

修改集合后,请输入以下代码:

Hello. Why don''t you have 2 collections?
I prefer to bind my grid to a collection, thereafter, if I modify my collection, the grid will be change automatically.

After modifing the collection put this code:

YourDataGrid.ItemsSource = YourCollection;



然后网格将刷新.



then the grid will refresh.


这篇关于在DataGrid中刷新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆