Sunday, December 26, 2010

WPF Datagrid Demo

I have developed a project on WPF before. WPF has been utilized in Vista OS and other software. But this cannot replace the stability of Windows Forms.
Still, I don't have ample time to dive deeply in WPF, hopefully soon! LOL.. :D
So, to begin with:
1. Download and install WPF Toolkit (Visual Studio 2008)
2. Open Visual Studio 2008
3. Add a WPF Project (Just call it GridDemo)
4. Add a DataGrid in your Window.xaml
5. Make sure to have referenced wpf toolkit: http://schemas.microsoft.com/wpf/2008/toolkit/
6. Customize Your Grid
1:  <grid:DataGrid AutoGenerateColumns="False" Margin="8,11,10,78"  
2:  ItemsSource="{Binding}" Name="CusGrid">  
3:  <grid:DataGrid.Columns>  
4:  <grid:DataGridTextColumn Binding="{Binding Path=CustomerID}"  
5:  Header="Customer ID" />  
6:  </grid:DataGrid.Columns>  
7:  ....other columns goes here  
8:  </grid:DataGrid>  
7. In your Window1.cs file, populate your grid with resulset
Code:
 protected void GridLoad()  
 {  
  SqlConnection conn = new SqlConnection(your_connection_string);  
  SqlCommand cmd = new SqlCommand("select * from customers",conn);  
  DataSet ds = new DataSet();  
  SqlDataAdapter da = new SqlDataAdapter(cmd);  
  da.Fill(ds);  
  CusGrid.DataContext = ds.Tables[0];  
 }
8. Run your application.
That's It!

0 comments:

Post a Comment