A bit about the stuff I've done


Thursday 19 January 2012

Binding a datagridview to a list

Ok, so binding a grid to a list is easy, but what if you want changes in the list to be shown in the grid?
Well actually, it turns out, this is easy too - just not obvious.

Instead of using System.Collections.Generic.List<T>
use System.ComponentModel.BindingList<T>
It's exactly the same in all important regards, but it has the methods necessary for the grid to know that the list has changed

Easy huh!

Friday 13 January 2012

Editing xml in vim

Yes, I use vim!

And large html, xml and xsl documents can be frustrating so here are a few things I sussed out today to make it easier:

  1. None of the commandline tools I could find were capable of cleanly formatting an xml document and putting comments in the correct alignment. All of them put the comments with no indentation.
    Some of you may prefer this but it stosp the following tips from working correctly in vim
    So I wrote a quick 5 min tool to do this, and here it is.
    Extract that archive somewhere into your path and then simply cat unformatted.xml | xmltidy > formatted.xml
    easy :)
    N.B. You'll need to have mono installed.
  2. Next I've assigned this new tool to an easy key in vim
    In your .vimrc file enter the following:
    :map <F2> :1,$!xmltidy<Enter>
    You can use any key you like of course
  3. Next I enabled code-folding
    Now there are plenty of posts out on the web about getting code folding with xml to work however I had issues with all of them.
    What I found to work was to enable folding by indentation (hence why comments have to be in the correct alignment).
    These 2 lines of vimrc sorted that out for me:
    au FileType htm,html,xml.xsl,xslt exe ":set foldmethod=indent"
    au BufNewFile,BufRead *.htm,*.html,*.xml,*.xsl,*.xslt exe ":set shiftwidth=2"

    N.B. I have my tabstop set to 2 so that pressing the tabkey indents the line by 2 columns instead of the usual 
  4.  Finally; if, like me, you don't like that ugly banner on the code folding lines then you'll want to change the colours with this line:
    :highlight Folded ctermbg=black
Having enabled the code folding you can use zo and zc to open and close a fold respectively.
N.B. when folding it folds everything at that level, not everything that is a child.
In other words if you had this document:
<xml>   <node1>     <node1a/>     <node1b/>   </node1>   <node2>     <node2a/>     <node2b/>   </node2> </xml>
and you press zc when the cursor is on the line with node1 then rather than hiding node1a and node1b like you might expect - it hides node1 and node2 (as well as their children)
 


Distinct in XSL

So I frequently have a need to do this but I can never remember how, so I figure why not use my shiny new blog and write about it here!

The problem:
  • You have some node list which contains many, many items.
  • You want to group items based on part of their content
  • You want to do it in xsl
Example:
The xml:
<phrases>   <node type="Greeting">Hello</node>   <node type="Farewell">Goodbye</node>   <node type="Greeting">Hi</node>   <node type="Greeting">Howdy</node>   <node type="Farewell">See you later</node> </phrases>
the result:
Greeting Farewell
The solution:
You can use the generate-id() function.
This function is part of xsl so you don't need any funky includes, but it means you can't use it from XPath (e.g. from C#'s SelectNodes() method)

<xsl:if test="generate-id(.) = generate-id(../node[@type=current()/@type])>   <xsl:value-of select="."/> </xsl:if>

And that's it!
Let's break it down

  • generate-id(.)
    Does exactly what you might expect i.e. it generates a unique ID which represents the current node. Calling generate-id on the same node twice will return the same id.
  • ../node[@type=current()/@type]
    This bit gets all the other nodes which have the same value based on the key we are grouping by (in this case

Visit my webites

As well as this blog I have 2 main websites currently.

  • My development website which lists the personal software projects I have developed
    http://dev.dj-djl.com
I've also got a new site, still under construction dedicated to the radio switcher project which has been my main focus of late.
http://dev.dj-djl.com/radioswitcher.aspx

When this project goes live I will probably give it its own domain name

Welcome to my blog

I've just created this blog today so bare with me while the content slowly builds!
I guess the idea behind this blog is to share some of the cool things I've done or discovered in the development world.
Hopefully someone somewhere will find it useful!