Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void button1_Click(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";
 

 
            DataGrid dg2 = new DataGrid();
 
            
 
          
            try
            {
                connection.Open();
                oledbAdapter = new OleDbDataAdapter(sql, connection);
                oledbAdapter.Fill(dt);
                dg2.DataContext = dt;
                connection.Close();
 

 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
 
            dg2.HorizontalAlignment = HorizontalAlignment.Left;
            dg2.VerticalAlignment = VerticalAlignment.Top;
            dg2.Height = 700;
            dg2.Width = 900;
            dg2.Background = Brushes.YellowGreen;
            grid1.Children.Add(dg2);
           
 
        }


this code instantiate a DataGrid dg2 and expected to show the content of Database Book . but only a Blank DataGrid form is shown. somewhere i have to do the DataBinding and how I can do it in the codebehind.

Any help will be appreaciated
Posted
Updated 25-Jan-12 5:00am
v2

1 solution

Hi Mr.MohammadIqbal,

You pls write a statement
dg2.ItemsSource = dt.DefaultView; instead of dg2.DataContext = dt;

--
Enjoy.
 
Share this answer
 

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