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";
 
<pre>
        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 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 325
2 ProgramFOX 265
3 Maciej Los 245
4 Andreas Gieriet 200
0 OriginalGriff 465
1 Sergey Alexandrovich Kryukov 275
2 ProgramFOX 265
3 Maciej Los 245
4 Andreas Gieriet 200


Advertise | Privacy | Mobile
Web01 | 2.8.150331.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