TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
jnmsy
NA
46
4.3k
Databinding to Datagrid using ObjectDataProvider in WPF
May 3 2012 10:01 AM
My requirement is I want to fetch data form database and will assign to some collection in a class and I want add that as object data provider in my UserControl..that data i want to assign it to Datagrid with my customized columns ..WHICH PROPERTY CAN I USE..?
<UserControl x:Class="FrontOffice2012.User.StudentsDetails"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:data="clr-namespace:FrontOffice2012.DB"
mc:Ignorable="d" d:DesignHeight="357" d:DesignWidth="765" MinHeight="255" MinWidth="700" Loaded="UserControl_Loaded">
<UserControl.Resources>
<ObjectDataProvider x:Key="objDs" ObjectType="{x:Type data:StudentDetails}" MethodName="GetStudents"></ObjectDataProvider>
<ObjectDataProvider x:Key="objDsBacthes" ObjectType="{x:Type data:StudentDetails}" MethodName="GetBatchNames"></ObjectDataProvider>
</UserControl.Resources>
<DockPanel>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="55"></RowDefinition>
<RowDefinition Height="217*"></RowDefinition>
<RowDefinition Height="73"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel HorizontalAlignment="Left" Width="568" Background="SteelBlue" Orientation="Horizontal" Margin="0,12,0,9">
<Label FontWeight="Bold" Foreground="Black" Margin="10,0,0,0" Height="27" Width="58">Batches</Label>
<ComboBox Width="200" Height="23" Name="cmbBatches" DataContext="{Binding Source={StaticResource objDsBacthes}}" ItemsSource="{Binding}" SelectionChanged="cmbBatches_SelectionChanged">
</ComboBox>
<Label FontWeight="Bold" Width="81" Margin="50,0,0,0" Foreground="Black" Height="26">Check State</Label>
<ComboBox Name="cmbCheckstate" Height="23" SelectionChanged="cmbCheckstate_SelectionChanged" Width="154">
<ComboBoxItem>All</ComboBoxItem>
<ComboBoxItem>Active</ComboBoxItem>
<ComboBoxItem>In Active</ComboBoxItem>
</ComboBox>
</StackPanel>
<DataGrid Grid.Row="1" Name="dgStudentdetails" RowBackground="LightYellow"
AlternatingRowBackground="LightBlue" CanUserAddRows="False" IsReadOnly="True" ItemsSource="{Binding Source={StaticResource objDs}}"
AutoGenerateColumns="True" >
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Name="mnuAdd" Header="Add New Student" Click="btnAdd_Click"></MenuItem>
<MenuItem Name="mnuEdit" Header="Edit Student Details" Click="btnEdit_Click"></MenuItem>
<MenuItem Name="mnuChangeStudentBatch" Header="Change Student Batch" Click="btnChangeStudentBatch_Click"></MenuItem>
<MenuItem Name="mnuCollectFee" Header="Collect Fee" Click="btnCollectfee_Click"></MenuItem>
<MenuItem Name="mnuPrintReceipt" Header="Print Receipt" Click="btnPrintreceipt_Click"></MenuItem>
<MenuItem Name="mnuCancelReceipt" Header="Cancel Receipt" Click="btnCancelreceipt_Click"></MenuItem>
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.Columns>
<DataGridTextColumn Visibility="Hidden" Binding="{Binding Source=PKStudentId}"></DataGridTextColumn>
<DataGridTextColumn Header="Registration Number" Binding="{Binding Path=RegistrationNumber}" Width="220*" IsReadOnly="True"></DataGridTextColumn>
<DataGridTextColumn Header="Student Name" IsReadOnly="True" Binding="{Binding Path=StudentName}" Width="180*" ></DataGridTextColumn>
<DataGridTextColumn Header="Amount Paid" Width="200*" IsReadOnly="True" Binding="{Binding Path=AmountPaid}" >
</DataGridTextColumn>
<DataGridTextColumn Header="Amount To Be Paid" Width="180*" Binding="{Binding Path=AmountToBePaid}" IsReadOnly="True" ></DataGridTextColumn>
<DataGridTemplateColumn Header="Active" IsReadOnly="True" Width="100*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox HorizontalAlignment="Center" IsChecked="{Binding Path=Active}" IsEnabled="False" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="RegistrationDate" IsReadOnly="True" Width="140*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker HorizontalAlignment="Center" SelectedDate="{Binding Path=RegistrationDate}" Foreground="Red" Background="Yellow" IsEnabled="False" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<StackPanel Orientation="Horizontal" Background="SteelBlue" DockPanel.Dock="Bottom" HorizontalAlignment="Right" Grid.Row="2" Margin="0,15,0,18" Width="736">
<Button Width="106" Margin="10,0,10,0" FontWeight="Bold" Name="btnAdd" Height="25" Click="btnAdd_Click" >Add</Button>
<Button Width="106" Margin="0,0,10,0" Name="btnEdit" FontWeight="Bold" Height="25" Click="btnEdit_Click" >Edit</Button>
<Button Width="135" Margin="0,0,10,0" FontWeight="Bold" Name="btnChangeStudentBatch" Height="25" Click="btnChangeStudentBatch_Click" >Change Student Batch</Button>
<Button Width="106" Margin="0,0,10,0" FontWeight="Bold" Click="btnCollectfee_Click" Height="25" Name="btnCollectfee" >Collect Fee</Button>
<Button Width="106" Margin="0,0,10,0" FontWeight="Bold" Name="btnPrintreceipt" Height="25" Click="btnPrintreceipt_Click" >Print Receipt</Button>
<Button Width="106" Margin="0,0,10,0" FontWeight="Bold" Name="btnCancelreceipt" Height="25" Click="btnCancelreceipt_Click" >Cancel Receipt</Button>
</StackPanel>
</Grid>
</DockPanel>
</UserControl>
*******************************
Class for ObjectDataProvider
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrontOffice2012.DB;
namespace FrontOffice2012.DB
{
public class StudentDetails
{
private CompanyDataContext dc = new CompanyDataContext();
private IEnumerable<StudentData> _StudentsCollection;
public IEnumerable<StudentData> StudentsCollection
{
get { return _StudentsCollection; }
set { _StudentsCollection = value; }
}
public IEnumerable<StudentData> GetStudents()
{
var result = (from a in dc.Students
select
new StudentData
{
PKStudentId = a.PKStudentId,
RegistrationNumber = a.RegistrationNumber,
StudentName = a.StudentName,
AmountPaid = a.AmountPaid,
AmountToBePaid = a.AmountToBePaid,
Active = a.Active,
RegistrationDate = a.RegistrationDate
});
return result;
}
public IEnumerable<BatchName> GetBatchNames()
{
var result = (from a in dc.Batches select new BatchName {Batchname = a.BatchName});
return result;
}
public IEnumerable<StudentData> GetStudentDetailsByStatus(int selind)
{
CompanyDataContext dc = new CompanyDataContext();
switch (selind)
{
case 2:
var result1 = (from a in dc.Students
where a.Active == false
select new StudentData
{
PKStudentId = a.PKStudentId,
RegistrationNumber = a.RegistrationNumber,
StudentName = a.StudentName,
AmountPaid = a.AmountPaid,
AmountToBePaid = a.AmountToBePaid,
Active = a.Active,
RegistrationDate = a.RegistrationDate
});
return result1;
break;
case 1:
var result2 = (from a in dc.Students
where a.Active == true
select new StudentData
{
PKStudentId = a.PKStudentId,
RegistrationNumber = a.RegistrationNumber,
StudentName = a.StudentName,
AmountPaid = a.AmountPaid,
AmountToBePaid = a.AmountToBePaid,
Active = a.Active,
RegistrationDate = a.RegistrationDate
});
return result2;
break;
default:
return GetStudents();
break;
}
}
}
public class StudentData
{
public int PKStudentId;
public string RegistrationNumber;
public string StudentName;
public decimal? AmountPaid;
public decimal? AmountToBePaid;
public bool? Active;
private DateTime? _RegistrationDate;
public DateTime? RegistrationDate
{
get { return _RegistrationDate; }
set { _RegistrationDate = value; }
}
}
public class BatchDetailsODP
{
private CompanyDataContext dc = new CompanyDataContext();
private IEnumerable<string> _BatchNames;
public IEnumerable<string> Batchnames
{
get { return _BatchNames; }
set { _BatchNames = value; }
}
}
public class BatchName
{
public string Batchname;
}
}
Reply
Answers (
0
)
How to Set the size of combo box dropdown list ?
Resize and Edit Text in wpf