WPF gridview binding coding with sample project for desktop application [Resolved]

Posted by Joewilliam under WPF on 6/15/2011 | Points: 10 | Views : 6480 | Status : [Member] | Replies : 1
WPF gridview binding coding with sample project for desktop application

William Mary


Responses

Posted by: Ndebata on: 6/16/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
IN XAML

<DataGrid IsReadOnly="True" AutoGenerateColumns="False" Width="260" x:Name="mygrid" CanUserAddRows="False" CanUserDeleteRows="False" AlternatingRowBackground="AliceBlue" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=EmployeeID}" Header="EmpID">
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Path=Name}" Header="Employee Name">
</DataGridTextColumn>
<DataGridTemplateColumn Header="DOB">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker x:Name="dobPicker" SelectedDate="{Binding Path=DOB}"></DatePicker>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

In .cs file i have class called Employee
public class Employee

{
public string Name { get; set; }
public DateTime DOB { get; set; }
public int EmployeeID { get; set; }
}

Then you can set the collection as ItemSource of GridView

generating some sample data.
List<Employee> lstemp = new List<Employee>();
lstemp.Add(new Employee { Name = "Narayan", DOB = new DateTime(2011, 1, 1), EmployeeID = 1 });
lstemp.Add(new Employee { Name = "Dot", DOB = new DateTime(2010, 1, 1), EmployeeID = 2 });
lstemp.Add(new Employee { Name = "Debata", DOB = new DateTime(2001, 1, 1), EmployeeID = 3 });
lstemp.Add(new Employee { Name = "Hello", DOB = new DateTime(2005, 1, 1), EmployeeID = 4 });
mygrid.ItemsSource = GetEmpList();


Joewilliam, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response