Click here to Skip to main content
11,928,078 members (50,972 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# 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();
                oledbAdapter = new OleDbDataAdapter(sql, connection);
                dg2.DataContext = dt;
            catch (Exception ex)
            dg2.HorizontalAlignment = HorizontalAlignment.Left;
            dg2.VerticalAlignment = VerticalAlignment.Top;
            dg2.Height = 700;
            dg2.Width = 900;
            dg2.Background = Brushes.YellowGreen;

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

1 solution

Rate this: bad
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;


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.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