We will use SQL Server 2008 for making our database and table and create our design for DataGrid in XAML format. After that bind that DataGrid in xaml.cs file.
Initial chamber Step 1: Open Visual Studio 2010, Go to File, then New and click Projects. Under Visual C#, select WPF Application.
Design chamber In the Solution Explorer there is a Main Window.xaml file. You will see something like the following mage.
Here you have to drag and drop the DataGrid from the toolbox. Initially there will be no columns in the DataGrid. For that you have to write the following code to make columns.
You have to write the following code where XAML is written and all code should be in between
<grid> </grid>.
- <Window x:Class="WpfApplication3.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MainWindow" Height="350" Width="525">
- <Grid>
- <DataGrid AutoGenerateColumns="False" Height="217" HorizontalAlignment="Left" Margin="73,33,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="364">
-
- <DataGrid.Columns>
-
- <DataGridTextColumn Header="ID" Width="40" Binding="{Binding id}"></DataGridTextColumn>
- <DataGridTextColumn Header="FirstName" Width="100" Binding="{Binding fname}"></DataGridTextColumn>
- <DataGridTextColumn Header="LastName" Width="100" Binding="{Binding lname}"></DataGridTextColumn>
- <DataGridTextColumn Header="City" Width="100" Binding="{Binding city}"></DataGridTextColumn>
-
-
- </DataGrid.Columns>
-
- </DataGrid>
-
-
-
- </Grid>
- </Window>
Your design will look like the following screenshot:
Database chamber In the database, I had made my table in SQL Server 2008. You can take help from local database or SQL Service based database and create a table:
Add New Table, then tbl_data.
After creating the table, you have to feed some data in the table, otherwise you will get empty Datagrid at run time.
Code chamber In this step right click inside the Main Window.XAML, then click View Code. You will get inside the coding part, similar to page.aspx.cs file; here the file name is Main Window.xaml.cs.
Main Window.xaml.cs - using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Data;
- using System.Data.SqlClient;
-
- namespace WpfApplication3
- {
-
-
-
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- refreshdata();
- }
-
- public void refreshdata()
- {
- SqlConnection con = new SqlConnection(@"Data Source=NiluNilesh;Integrated Security=True");
- con.Open();
- SqlCommand cmd = new SqlCommand("select * from tbl_data", con);
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- Database1DataSet ds = new Database1DataSet();
- sda.Fill(ds);
- if (ds.Tables[0].Rows.Count>0)
- {
- dataGrid1.ItemsSource = ds.Tables[0].DefaultView;
- }
- con.Close();
- }
- }
- }
Output chamber Hope you liked this. Have a good day. Thank you for reading.