Datagrid Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Abhi

    #1

    Datagrid Problem

    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>co m

  • Otis Mukinfus

    #2
    Re: Datagrid Problem

    On 28 Jan 2006 03:35:11 -0800, "Abhi" <abhi.forum@gma il.com> wrote:
    [color=blue]
    >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>c om[/color]
    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.Collecti ons.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.WriteLi ne(newString);
    }
    else
    {
    Console.WriteLi ne(s);
    }
    }
    Console.ReadLin e();
    }
    }
    }

    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


    Comment

    Working...