Window Presentation Foundation How To Bind The Grid Dynamically In Wpf. I want to Express My Thought Through Coding Please Understand. if you have any dought please Send Me Your Query. This is Simple Example I have taken one List Box and Fireing One Event on Window Load Event.
First Of All You Need To Create The Database, In This Sample Application I am Using Sql Server. My Database Name is Test That Contain table and table Contain Some Attribute Name Is EmpId, Ename, Address and My Table Name is tblEmp. So, Viewer you need to do This part i am unable to upload my sample application
After That This Couple Of Line You write in Window1.XAML
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="280" Width="563" Loaded="Window_Loaded">
<Window.Resources>
<DataTemplate x:Key="listBoxTemplate">
<StackPanel Margin="4">
<DockPanel >
<TextBlock FontWeight="Bold" Text="EmpIdentification:"
DockPanel.Dock="Right"
Margin="5,0,10,0"/>
<TextBlock Text=" " />
<TextBlock Text="{Binding EId}" Foreground="Green" FontWeight="Bold" />
</DockPanel>
<DockPanel >
<TextBlock FontWeight="Bold" Text="Name:" Foreground ="DarkOrange"
DockPanel.Dock="Left"
Margin="5,0,5,0"/>
<TextBlock Text="{Binding Ename}" />
</DockPanel>
<DockPanel>
<TextBlock FontWeight="Bold" Text="Address" Foreground="RoyalBlue"
DockPanel.Dock="Left"
Margin="5,0,15,0"/>
<TextBlock Text="{Binding Address}"/>
</DockPanel>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid Margin="49,12,29,0" Name="grid1" Height="221" VerticalAlignment="Top" Width="467">
<ListBox Margin="17,8,15,26" Name="lstnox1" ItemsSource="{Binding Tables[0]}"
ItemTemplate="{StaticResource listBoxTemplate}" />
</Grid>
</Window>
And after That You Need To Open Your Window1.XAML.CS and put These Couple Of Lines
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.SqlClient;
using System.Data;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public SqlConnection connection;
public SqlCommand command;
string sql = "SELECT EmpId,Ename,Address FROM tblEmp";
string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Test.mdf;Integrated Security=True;User Instance=True";
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
BindGrid();
}
private void BindGrid()
{
DataSet dtSet = new DataSet();
using (connection = new SqlConnection(connectionString))
{
command = new SqlCommand(sql, connection);
SqlDataAdapter adapter = new SqlDataAdapter();
connection.Open();
adapter.SelectCommand = command;
adapter.Fill(dtSet, "tblEmp");
lstnox1.DataContext = dtSet;
}
}
}
}
This is Code For Dynamically You Can Bind The Grid In WPF And You Out Put Look Like
Cheers
Bindas.....
No comments:
Post a Comment