Pages

Tuesday, August 28, 2012

C# DataTable Order By Column


Sometimes when you work with DataSet or DataTable you need to order the data by specific column. The quickest way to sort DataTable by specific column is to convert it to DataView, apply the Sort method and then save back the results to your DataTable. Here is the code sample:
//   Create DataTable and fill out with data
DataTable table = new DataTable();  
table.Columns.Add("EmployeeFirstName", typeof(string));  
table.Columns.Add("EmployeeFamilyName", typeof(string));  
table.Columns.Add("Age", typeof(int));
DataRow row = table.NewRow();
  
row["EmployeeFirstName"] = "Bill";  
row["EmployeeFamilyName"] = "Gates";  
row["Age"] = 56;  
table.Rows.Add(row);  

row = table.NewRow();  
row["EmployeeFirstName"] = "Steve";  
row["EmployeeFamilyName"] = "Ballmer";  
row["Age"] = 57;  
table.Rows.Add(row); 

row = table.NewRow();  
row["EmployeeFirstName"] = "Paul";  
row["EmployeeFamilyName"] = "Allen";  
row["Age"] = 59;  
table.Rows.Add(row); 

//  In real life applications, it is critical 
//  to check if your DataTable is not empty!
if (table.Rows.Count > 0)  
{
    //  Convert DataTable to DataView
   DataView dv = table.DefaultView;
   //   Sort data
   dv.Sort = "Age";
   //   Convert back your sorted DataView to DataTable
   table = dv.ToTable();  
}

Wednesday, August 22, 2012

WPF ScrollViewer Control Example


There are two controls that enables scrolling in WPF: ScrollBar and ScrollViewer. The ScrollViewer encapsulates both vertical and horizontal ScrollBar and a container that contains all the visible elements inside scrollable area. I'll show a ScrollBar example based on code described in my previous post.
WPF ScrollViewer example

Tuesday, August 21, 2012

WPF Expander with DataGrid


WPF has a very useful control called Expander. Expander allows you to hide or show any content that is inside of it. Today I'll show you how to use Expander with DataGrid and how to apply a Style to Expander's Header.
Collapsed WPF Expander with style and DataGrid inside of it
Collapsed state
Expanded WPF Expander with style and DataGrid inside of it
Expanded state

Saturday, August 11, 2012

C# Write to Windows Event Log


As you probably know Windows has its own Event Logging System. You can see the logs in it if you'll run Event Viewer from Administrative Tools. Today I'll show how to write messages into Windows Event Log from your application using C#.
Microsoft Windows Event Viewer

C# LINQ - OrderBy clause

This post is part of a series called LINQ Examples that brings practical examples of using LINQ in C#.

Today we'll see practical examples of using OrderBy, ThenBy clauses in LINQ. This kind of operators in LINQ are called LINQ Ordering Operators.
List<Employee> employees = GetEmployeeList();

var sortedEmployees =
    from e in employees
    orderby e.EmployeeName
    select e;
Console.WriteLine("Employees list ordered by name:");
foreach(var e in sortedEmployees)
    Console.Write(e.EmployeeName + ", ");    /*    Output:    John, Lucas, Marina, Susan, Tomas   */

Saturday, August 4, 2012

C# Entity Framework in Visual Studio 2010

Some time ago I published an article about LINQ to SQL Classes Generation in Visual Studio 2010. Today I'll show how to use a newer version of Microsoft ADO.NET technology called Entity Framework.
ADO.NET Entity Framework enables developers to work directly with object model of a database instead of writing SQL queries. Developers which  already worked with LINQ to SQL will easily switch to Entity Framework, because the overall using of it is pretty similar.

Ok, let's move on to the example. I created a WPF project with a datagrid in the main window and a simple local database with one single table Employees. Now I want to use ADO.NET Entity Framework to fetch data from the database and show it in my datagrid. Final result will look like this:

C# WPF Datagrid with ADO.NET Entity Framework