Devexpress ASPxPivotGrid Templates

A very useful piece of code for templating over field values and field headers in a Devexpress pivot grid

public class FieldValueTemplate_AbsColHeader 
{
    public FieldValueTemplate_AbsColHeader() { }

    public void InstantiateIn(Control container)
    {
        var c = (PivotGridFieldValueTemplateContainer)container;
        var cell = c.CreateFieldValue();

        var b = new ASPxLabel()
        {
            Text = cell.TextControl.Text.Split('|')[0],
            EnableDefaultAppearance = false,
            ClientInstanceName = cell.TextControl.Text.Replace("|", "_")
        };

        cell.Controls.AddAt(cell.Controls.IndexOf(cell.TextControl), b);
        cell.Controls.Remove(cell.TextControl);
        c.Controls.Add(cell);
    }
}

Continue reading

C# Appending to a File

As I have been mainly developing for the web … it’s easy to loose track of the simple things like this:

    string path = @"c:\temp\MyTest.txt";

    // This text is added only once to the file.
    if (!File.Exists(path)) 
    {
        // Create a file to write to.
        using (StreamWriter sw = File.CreateText(path)) 
        {
            sw.WriteLine("Hello");
            sw.WriteLine("And");
            sw.WriteLine("Welcome");
        }	
    }

    // This text is always added, making the file longer over time
    // if it is not deleted.
    using (StreamWriter sw = File.AppendText(path)) 
    {
        sw.WriteLine("This");
        sw.WriteLine("is Extra");
        sw.WriteLine("Text");
    }	

    // Open the file to read from.
    using (StreamReader sr = File.OpenText(path)) 
    {
        string s = "";
        while ((s = sr.ReadLine()) != null) 
        {
            Console.WriteLine(s);
        }
    }