MexiCode Ideas en codigo

Formatting DataGridView cell with custom DataSource

Posted on January 13, 2010

When you use a custom DataSource (in my case a generic list) the properties become the columns and the property names, become the column header.

I needed to format a DateTime field, so I did this:

1
2
3
4
5
6
7
8
9
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "Date")
    {	
        DateTime value = (DateTime)e.Value;
        e.Value = value.ToString("G");
        e.FormattingApplied = true;
    }
}

In this case, "Date" is the property name of a DateTime field.

In case you are curious, the DataSource assignment is like this:

1
2
3
List<GoodReceipts> grrs = new List<GoodReceipts>();
grrs.AddRange(logic.GetGRRData(consumptions, validSince, validTo));     
dataGridView1.DataSource = grrs;

Connect PHP and Sql Server (ntwdblib.dll)

Posted on January 12, 2010

If you need to connect PHP with SQL Server (in a Windows installation), go to your PHP install folder, and replace the file ntwdblib.dll with this one.

Apparently, PHP is not allowed to include this Dll without getting sued or something.

Tagged as: , No Comments

Socks Proxy via SSH Tunneling

Posted on January 9, 2010

Probably, everyone and their dog knows how to set up a ssh tunnel for safe webbrowsing.

Well, I didn't know, so I'm writing how to do it.

I have a SSH account on a webserver somewhere (I'm not telling). It's really simple, log in to your account with a local port that forwards all traffic to your ssh connection. Do you know how to do it? no? Well..

Windows
start putty.exe -D 1080 -ssh servername -l username -pw password

Linux
ssh username@servername -D 1080

If you don't have putty, shame on you. Download it here

Now, open your webbrowser of choice, and edit the proxy information:

Address: 127.0.0.1 (thats your own computer)
Port: 1080
Proxy Type: SOCKSv5

And that should do it.

Now, here's an extra tip for all of you who use Firefox. We can do all of our DNS Lookups using our proxy (Effectively overriding any dns webfilter your company may use).

Open about:config and look for network.proxy.socks_remote_dns value and change it to true. This only works with SOCKSv5.

Enjoy :D

Serializing Classes

Posted on January 4, 2010

I stumbled upon a new problem. Again.

In a project I'm currently involved in, I had to use serializable classes. The application saves the serialized object in a database, and then, another application can retrieve the object and deserialize it.

Or at least, that's what i thought.

I was doing some "cut&paste" programming, and I thought it would be easier to just have 2 copies of the serializable class in two different projects. (this one)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using System.Collections.Generic;
 
namespace Trivius.Wix.ORM
{
    [Serializable]
    public class Message
    {
 
        [Serializable]
        public class ColorMessage
        {
            public string Message { get; set; }
            public byte Color { get; set; }
        }
 
        public byte Effect { get; set; }
        List<ColorMessage> colorMessages = new List<ColorMessage>();
 
        public void AddMessage(ColorMessage msg)
        {
            colorMessages.Add(msg);
        }
 
        public List<ColorMessage> GetMessages()
        {
            return colorMessages;
        }            
    }    
}

In one application, this approach worked beautifully. It saved and retrieved from the database like a charm. But when I tried to deserialize the object in another application (using the other copy of the exact same class) the compiler told me that:

Cannot cast object of type Trivius.Wix.Message into an object of type Trivius.Wix.Message

Not nice.

So, what I did, is that I separated the serializable class into a Dll, and added a reference to that same class in both projects, and it worked.

I should have done that in the first place. :(