Datagrid Problem

  • Thread starter Thread starter Abhi
  • Start date Start date
A

Abhi

Hi all,

I am stuck in a situation where I want to sort the datagrid column
in numerical order where it originally is in string format.

For example suppose there are 3 fields id, name, age in datagrid
where all are in string format. But in 'id' coloumn there are numbers
like 1, 2 .... and also string data like A0001, B0034, and so on.. Now
my problem is to somehow tell .Net that whenever possible, consider the
data in 'id' field as Number and sort it accordingly. Rest of the data
can be sorted in String format as it does by default.

Guys, please help me if you can ... Any help is welcomed ...

Thanks in advance :-)
Abhi Win
abhi<dot>forum<at>gmail<dot>com
 
Hi all,

I am stuck in a situation where I want to sort the datagrid column
in numerical order where it originally is in string format.

For example suppose there are 3 fields id, name, age in datagrid
where all are in string format. But in 'id' coloumn there are numbers
like 1, 2 .... and also string data like A0001, B0034, and so on.. Now
my problem is to somehow tell .Net that whenever possible, consider the
data in 'id' field as Number and sort it accordingly. Rest of the data
can be sorted in String format as it does by default.

Guys, please help me if you can ... Any help is welcomed ...

Thanks in advance :-)
Abhi Win
abhi<dot>forum<at>gmail<dot>com
Abhi,

My solution would be to do something like this when populating the
grid, but be aware that if you leave the last item in the array the
program will fail when it tries to convert "1C000" to an integer. I
left it in there to demonstrate that you will have to handle rows with
something like this in them.

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace TestConsole
{
class Program
{
static void Main(string[] args)
{

string[] strings = { "B0001", "1", "11", "1C000" };
string newString = string.Empty;

foreach (string s in strings)
{
char c = (char)s[0];
if (char.IsDigit(c))
{
newString = string.Format("{0:00000}", Convert.ToInt32(s));
Console.WriteLine(newString);
}
else
{
Console.WriteLine(s);
}
}
Console.ReadLine();
}
}
}

If you don't want to use leading zeros in the sort column you will
have to figure out another way to do it.


Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 
Back
Top