Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Kindly help the novice man,

I am all records from table 'PersonnelDetails'. this i want to display using datagrid at run time.
the datagrid is a child of a dockpanel in a WPF app. When i run this code i only see one culoumn of the datagrid without any label or data:
public void viewAllStaffMethod(object sender, RoutedEventArgs e)
            {
                var dGrid = new DataGrid { };


                DockPanel dockPanelmain = new DockPanel();
                dockPanelmain.Children.Add(dGrid);
                string conString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\MOICIDB.mdf;Integrated Security=True";
                string cmdString = string.Empty;
                using (SqlConnection con = new SqlConnection(conString))
                {
                    cmdString = " select * from PersonnelDetails";
                    SqlCommand com = new SqlCommand(cmdString, con);
                    SqlDataAdapter sda = new SqlDataAdapter(com);
                    DataTable dt = new DataTable("PersonnelDetails");
                    sda.Fill(dt);
                    dGrid.ItemsSource = dt.DefaultView;

                }

plz note i dont want use databinding of WPF. I want to do it at run time with full control.

MAJ
Posted
Updated 11-Mar-14 3:30am
v3

1 solution

Hi, Just change the last line of code.

C#
dGrid.DataSource = dt;



Thanks & Regards,
Anand. G
 
Share this answer
 
Comments
Member 10645876 12-Mar-14 5:38am    
Mr. Anan i hav tried that before but DataSource is not a property of datagrid in WPF. it seems its only available with winforms

MAJ
Anand Gunasekaran 12-Mar-14 13:42pm    
Sorry, for the Wrong Answer. May be the following code is work.
dataGrid1.ItemsSource = dt.DefaultView;
Otherwise, The following link has that solution
http://www.codeproject.com/Articles/30905/WPF-DataGrid-Practical-Examples

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