Click here to Skip to main content
11,720,785 members (83,407 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF C#4.0 DataGrid
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
Posted 25-Jan-12 8:35am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
MohammadIqbal at 25-Jan-12 14:56pm
   
Thanks but I did it with dt.clear();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 2,179
1 OriginalGriff 924
2 F-ES Sitecore 640
3 Maciej Los 609
4 Richard MacCutchan 540


Advertise | Privacy | Mobile
Web03 | 2.8.150901.1 | Last Updated 25 Jan 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100