Saturday, December 8, 2007

Sorting a Generic List of custom type objects in a one-liner - C#

Do I love the new features in C# 3.0!! Here is a sample. Imagine the days of implementing an IComparable or an IComparer on your custom type so they become 'sortable'. Compare that with the one-liner below :)

(Assume there is a class called opportunity that has a property called Properties, which returns a value type based on a column name and that there exist methods to give what column the user clicked on and what order did he want)

List = GetOpportunityList();
int ascending = GetOrder(); // This method will return 1 if ascending and -1 if
// descending
string columnName = GetSortedColumn(); // Get the name of the column to be sorted

// One-liner sort
opportunities.Sort((a, b) => ascending * ((IComparable)a.Properties [columnName] ).CompareTo(b.Properties[columnName]));

Isnt this real simple? Now, the additional benefit that we get out of this is the ability to pass in local variables (ascending and columnName in this case) into the lambda expression and thereby the comparison, which would have never been possible if I used the "implementing IComparable or IComparer" route.

Happy Linqing!!

Thursday, July 5, 2007

Host ActiveX controls in Managed World

   Recently I embarked into the exercise of hosting some ActiveX controls inside of a windows form. Found out that  AxImp.exe was the way to go. Its a neat tool using which you can create a wrapper for the ActiveX control as a dll or as a C# file. After you create the wrapper for the ActiveX control as a C# file (emitted as a Winform user control that derives from AxHost), you can add attributes on the control to make it appear in toolbox. I tried this to host the Outlook List View control in a winform. Works like a charm :) More details on the usage of the tool can be found here.

Saturday, April 28, 2007

Custom formatting in DataAdapter.Update

Here is the scenario. I have a datatable that has a datetime column and i am using DataAdapter.Update(dataTable) method, to persist the changes back to the database. The default implementation of this method, for some reason, truncates the milliseconds when saving the values to the database. How do I get around this, but still leverage the ease-of-use and performance that the DataAdapter provides? Here is one possible (and simple) solution.

private void WriteChanges(DataTable dataTable )
{
aeAdapter.RowUpdating += new SqlCeRowUpdatingEventHandler(adapter_RowUpdating);
aeAdapter.Update(copyTable);
}

void adapter_RowUpdating(object sender, SqlCeRowUpdatingEventArgs e)
{
e.Command.Parameters["@Updated"].Value = FormatDate(e.Row["Updated"]);
}


private string FormatDate(object datetime)
{
if (datetime == null Convert.IsDBNull(datetime)) return "";
DateTime value = Convert.ToDateTime(datetime);
return value.ToString (System.Globalization.DateTimeFormatInfo.InvariantInfo) + "." + value.Millisecond.ToString();
}

Saturday, March 10, 2007

Hosting .Net Controls on Outlook Form Regions

If i am not wrong, VSTO v3 is going to support hosting .Net controls on outlook form regions. But, if you cant wait until then, you can try the ActiveX controls host thats posted on this yahoo group.
http://tech.groups.yahoo.com/group/vsnetaddin/files/VS.NET%20automation%20Shim%20Control/

I have seen this work. Worth a try.

Wednesday, February 21, 2007

This is cool


 

Posted this using OneNote 2007 and Word 2007

Custom formatting in DataGridView

I have a dataset that is returned by a web service. It contains a date column, among other columns, and this date is an UTC date(so, GMT time). Now, only when i display it in a datagridview, i need to display the local time depending on the user's local regional settings. Here is what I ended up doing....

// Bind the Cell formatting event
dgvGrid.CellFormatting += new DataGridViewCellFormattingEventHandler(dgvGrid_CellFormatting);


// Formatting event handler
void dgvGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

{
if( e.Value.GetType() == typeof(DateTime) )

{
e.CellStyle.Format = "G";
// This is the key. Converts GMT time to local time by adding the offset.
e.Value = ((DateTime)e.Value).ToLocalTime();

}
}

Started...

I had nothing to do.. I had been wanting to blog for a long time now.. And fate just tied these two together. Here goes my first post.