Tuesday, June 5, 2007

Adding sort direction arrow into GridView

Call this function @ RowCreated event:
public static void AddSortingArrow(GridView gv, GridViewRowEventArgs e)
{
  if (e.Row.RowType != DataControlRowType.Header)
    return;
  foreach (TableCell tc in e.Row.Cells)
  {
    if (tc.HasControls())
    {
      LinkButton lnk = (LinkButton)tc.Controls[0];
      if (lnk != null)
      {
        Label lbl = new Label();
        lbl.Text = (gv.SortDirection == SortDirection.Ascending ? "▲" : "▼");
        if (gv.SortExpression == lnk.CommandArgument)
        {
            lbl.Text = " " + lbl.Text;
            tc.Controls.Add(lbl);
        }
      }
    }
  }
}

Wednesday, May 30, 2007

VC simple mem leak detection code

Add these lines into header:
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
Call this function @ entry point
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

Making std::pair work as the key of hash_map in VC

Simple hack:
namespace stdext
{
  template<typename T1, typename T2> inline size_t hash_value(const std::pair<T1, T2>& _Keyval)
  {
    return hash_value(_Keyval.first) * 196613 + hash_value(_Keyval.second);
  }
}