Click here to Skip to main content
16,015,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
C#
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

1 solution

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:

C#
YourDataGrid.ItemsSource = YourCollection;


then the grid will refresh.
 
Share this answer
 
Comments
[no name] 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900