Sunday, July 4, 2010

How to hide duplicate Column Entries GridView VS2008 using c#

The Code below will hide values of textbox and label controls with the same ID in a gridview, you should be able to modify the code easily enough to support other controls. This does however mean that you have to use TemplateFields not basic BoundFields. For example if you have a Template field and inside it you have a textbox called txtName and a Phone number field called txtPhone, you specify HideDuplicatesInColumns to "txtName,txtPhone" for example:

<cus:GridView2 runat="server" ID="grdTrainMeNonSuperSet" AutoGenerateColumns="false"
HideDuplicatesInColumns="lblExerciseShort,lblMuscleGroup" GridLines="Horizontal"
OnRowCommand="grdTrainMeNonSuperSet_RowCommand">

Before you do this you need to add this class to you appcode folder, and in the your asp.net page add a reference to it.

Example of the Reference
<%@ Register Namespace="ST.Web.Controls" TagPrefix="cus" % >



The Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Web.UI;

namespace ST.Web.Controls
{
///
/// Summary description for GridView2
///
public class GridView2 : GridView
{
public GridView2()
: base()
{

}

protected override void OnDataBound(EventArgs e)
{
base.OnDataBound(e);
}

string hideDuplicatesInColumns = string.Empty;HideDuplicatesInColumns
string[] columnsToHideDups;

public string
{
set
{
hideDuplicatesInColumns = value;
columnsToHideDups = hideDuplicatesInColumns.Split(Convert.ToChar(","));
}
get
{
return hideDuplicatesInColumns;
}
}

private int ConvertToInt(string value)
{
return Convert.ToInt32(value);
}

Dictionary watchList = new Dictionary();
protected override void OnRowDataBound(GridViewRowEventArgs e)
{
if (columnsToHideDups != null)
{
//Hide all the duplicate values in based on the selected settings
foreach (string c in columnsToHideDups)
{
Control cntrl = e.Row.FindControl(c);

string val = string.Empty;
string id = string.Empty;
string key = string.Empty;
if (cntrl is TextBox)
{
val = ((TextBox)cntrl).Text;
id = ((TextBox)cntrl).ID;
key = string.Format("{0}", id);
if (watchList.ContainsKey(key) && watchList[key] == val)
{
((TextBox)cntrl).Text = String.Empty;
continue;
}
else
{
watchList.Add(key, val);
watchList[key] = val;
}
}
if (cntrl is Label)
{
val = ((Label)cntrl).Text;
id = ((Label)cntrl).ID;
key = string.Format("{0}", id);
if (watchList.ContainsKey(key) && watchList[key] == val)
{
((Label)cntrl).Text = String.Empty;
continue;
}
else
{
watchList[key] = val;
}
}

}
}
base.OnRowDataBound(e);
}

protected override void OnRowCreated(GridViewRowEventArgs e)
{
base.OnRowCreated(e);
}
}
}

Wednesday, June 2, 2010

The Garbage Law

This is how I try to live my life. Was surprised it had a name, but thought I'd share it here.

Tuesday, May 11, 2010

Vitamine D for Hodgkins Lymphoma

Its all over the internet but I think its important enough to keep repeating. Vitamin D hinders lymphoma and a whole bunch of other cancers. So much so that this study found that people diagnosed in summer times seems to have a 20% better survival rate!!! So stock up on Vitamin D supplements, and if possible get in the SUN as much as possible!! (without, obviously, burning yourself and getting skin cancer, kinda defeats the purpose)

Thursday, March 18, 2010

Microsoft SQL - Return random number in Query in Microsoft SQL

After a lot of searching around for a random number generation function for Microsoft SQL views, and trying all kinds of different methods, and only finding very resource expensive methods for getting a random number for each line in a query result set, I stumbled on this very obvious solution in a forum. I unfortunately cannot remember the forum otherwise I would post the link but I figured it couldn't hurt posting this here.

We all know NEWID() function will return us a GUID, which is nicely random. So using this we can generate ourselves a nice random number.

Step 1. Convert the Guid to Binary using

Step 2. Great so we have random binary, which we can change into anything we basically want, so lets convert it to an in CAST(CAST(NEWID() AS VARBINARY) AS int)

Step 3. Ok we have a random number but it may be negative and we probably just need something positive, so lets do ABS(CAST(CAST(NEWID() AS VARBINARY) AS int))

Step 4. Last step is to limit our Random number, The mod operator will help us here so lets say we want a random number up to 1000 we go ABS(CAST(CAST(NEWID() AS VARBINARY) AS int)) % 1000

So if you have a Northwind database and run

Select ABS(CAST(CAST(NEWID() AS VARBINARY) AS int)) % 10000 as RANDOMNUMBER,* from dbo.Customers

You will get a random number for each row in the Customer table in the database

You can also use this in a View and it works perfectly.

Hope this makes someone else's search a bit less frustrating than mine was.

Thursday, February 11, 2010

Cars are better than trains

There are certain definite advantages to public transport, aside from the obvious environmental benefits, you don't have to worry about car maintenance, traffic (at least not the kind the makes you calves hurt from having to maintain constant clutch control) and the fact that you can read your paper while travelling to work instead of concentrating on the road.

My mind, the unfortunate cynic, complains that my bodies environment suffers from the various city folk, coughing in my face, that traffic isn't that bad (you need the exercise) and you can get the news from the radio and get the added benefit of music instead of endless advertising in the paper (And even if there is advertising on the radio, its sometimes more entertaining, would miss the cartoons though...). So the trump card is car maintenance, which boils down to money.

Now personally I'm starting to think that the money for a car is really the well justified. First and foremost its a health issue. There is endless propaganda that talks about it being good for the environment to use public transport, but every winter I have to fight off flu after flu because some prior victim of the public transport health hazard, sneezes or coughs on me.

There are also the benefits of having a car for weekend trips (which cost some money anyway might as well pay a big oil corporate than a big public transport corporate). You don't need to rush to catch trains for you relaxing trips because the car leaves when you want to leave, and the comport of being in your own personal space (and not being sneezed on) is a brilliant perk.

One of the best perks I believe is the personal space perk of driving a car. When you leave work and get in your car, its your space. Its almost like being half-way home already. Your 4 square meters of space, in which you can do (and unfortunately some people take this too far) whatever you want to. No attention seeking teenagers with attitude playing their hip-hop as loud as possible, because they don't get enough attention at home and so needs it from the public hordes (join a football club or something!!!). No-one coughing on your head. No-one eating like giraffe with a twig stuck between their teeth. Just you, your music, your time.

Yup, cars are better than trains any day of the week.

Sunday, January 31, 2010

Big night out

It really does amuse me to find what I see as big night out now a days. Many people will smirk at a 29 year old thinking about the good old days. However my first big night out was around 12 year ago so I figure I am starting to gain just a little right to.

When I was 18 a big night out was synonymous with alcohol. If there was brandy there was a party, something that didn't change much by the time I got to 23 when my taste shifted to rum (I since went back to brandy). Rum is an essential drink for a guy trying to pick up girls, since I found the brandy made me mellow, whiskey made you forget. Rum made you brave! Nothing like a bit of Dutch courage when approaching a girl surrounded by her friends each one looking at you as if you are just another bottom feeder trying to score. I would like to think I was just honestly looking for someone and just lacking the courage initially. A theory that eventually bore fruit for me as I met my lovely wife in such a night.

By the time I reached 25 a big night out usually involved a braai (BBQ), still involving drink but now it was quality we were after. One or Two premium branded drinks were all we needed, sometimes a bit more but the goal of the night was to chill with the very friends you met while having way to much to drink when you were just 3 or 4 years younger.

Last night at the age of 29 out big night out started with dinner, followed by going to a pub and having a drink there (we had a rebellious moment and bought a shot of sours each) We actually even went to a second pub where we could dance a bit. However most of the time was spent shaking our heads as the younger less domesticated crowd was checking each other out, trying to look intimidating and the occasional girl in the short skirt trying to shove infront of you to try and display there dominance over basically everybody else (Couldn't help notice that the fabric of some of their dresses looked just like my grand mothers old curtains... recycling I guess?). Sign... nothing has changed in 12 years... except the girls didn't wear old curtains.

I guess a big night out is one you can smile about afterwards, have laugh about. The difference is that as with everything, the experience of previous big nights outs, will teach you how to get through one without making a complete idiot of yourself.


Thursday, January 28, 2010

Train delay theory

On Monday we very proudly managed to walk out of our house very proud of the fact that we left a full 5 minutes before our train was due. We only need a minute to walk there so we would have plenty of time to get to the station, get our metro and align ourselves with where the train door would be in order do get a seat when the door opens.
We were thus surprised to see the train arriving just as we stepped out of the parking area of our block of flats. But no worries! We're used to doing this, we bolted my wife with high heels and me with laptop bag swinging around wildly. The old lady living at the cute cottage probably shaking her head in her morning observations as the van Heerden's came rushing past again. Can just imagine her shaking her head thinking the youth of today has no sense of time.
Alas however, we didn't make the train. As we rounded to corner onto the platform the doors closed in that dramatic way only train door can, with a dramatic fooop that makes it seem like they are about to embark on an interstellar journey.
Seeing as we had some time to kill now, we realized that it was actually the previous train running late that we missed but that our train was also delayed by 25 minutes. Since it is January in London and frigid, our minds immediately went to coffee. We weren't the only ones either, the little coffee shop on the platform suddenly had more customers than he has in a good week. One of our fellow coffee drinkers mentioned that it was a person running over the rails. I think that possibly next Monday we will again be in the coffee shop only this time our helper behind the coffee counter will red in the face and puffing at us "£2.50 for your latte please!".