Click here to Skip to main content
11,489,693 members (71,998 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF C#4.0 DataGrid
 
 private void button2_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";
 
            try
            {
                connection.Open();
                oledbAdapter = new OleDbDataAdapter(sql, connection);
                oledbAdapter.Fill(dt);
               
                // dataGrid1.DataContext = dt;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
 
            DataGrid dg2 = new DataGrid();
            dg2.Height = 200;
            dg2.Width = 200;
            dg2.AutoGenerateColumns = true;
            dg2.AlternatingRowBackground = Brushes.Black;
            dg2.Background = Brushes.Red;
            dg2.BorderBrush = Brushes.Pink;
            dg2.HorizontalGridLinesBrush = Brushes.Black;
            dg2.DataContext = dt;
            stackPanel1.Children.Add(dg2);
            
        }
 

it is expected that the code should display the DataGrid with its underlying Data. but it is not happening.
it gives only a Red back ground. no data at all .No Grid lines also. I am missing something very seriously. please i request somebody help

Regards

Iqbal
NB: I am just started learning
Posted 18-Jan-12 4:48am
Comments
Shahin Khorshidnia at 18-Jan-12 18:34pm
   
I don't see any binding.

1 solution

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

Solution 1

after

stackPanel1.Children.Add(dg2);

use

dg2.ItemsSource = Your Source

I think it can help.
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 530
1 OriginalGriff 238
2 Maciej Los 190
3 Andy Lanng 183
4 _duDE_ 141
0 Sergey Alexandrovich Kryukov 9,488
1 OriginalGriff 8,425
2 Sascha Lefèvre 3,344
3 Maciej Los 3,066
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 18 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