Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF C#4.0 DataGrid
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 25-Jan-12 5:59am
Edited 25-Jan-12 6:00am
v2

1 solution

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

Solution 1

Hi Mr.MohammadIqbal,
 
You pls write a statement
dg2.ItemsSource = dt.DefaultView; instead of dg2.DataContext = dt;
 
--
Enjoy.
  Permalink  

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

  Print Answers RSS
0 Maciej Los 295
1 OriginalGriff 273
2 Aajmot Sk 234
3 Marcin Kozub 205
4 Richard MacCutchan 200
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 25 Jan 2012
Copyright © CodeProject, 1999-2014
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