Click here to Skip to main content
15,884,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am retrieving one value from the database the value is like

1,dt3435,26/5/2012 ~ 2,dt738,27/5/2012 ~ 3,dt892,26/5/2012


Now I want to split the values by using that tilde(~) operator, then I want store that in an array then Again I want to split that array values using Comma(,). then I want to save the values into the grid like

1 dt3435 26/5/2012
2 dt738  27/5/2012
3 dt892  26/5/2012


How to do this one can anyone help me to solve this issue.

Thanks in Advance
Posted
Updated 24-May-12 9:16am
v2

You need to create a class with 2 variables
1. An int variable (a)
2. A string variable (b)

Then you need to maintain a List of this class. Let's say the name of class is X.

List<x> valuesToBind = new List();

C#
string[] tildeSeperated = VALUEFROMDB.split('~');

foreach(string s in tildeSeperated)
{
    string[] temp = s.split(',');
    X objTemp = new X();
    objTemp.a= Convert.ToInt32(temp[0]);
    objTemp.b = temp[1];
    valuesToBind.Add(objTemp);
}
YOURDATAGRID.DataSource = valuesToBind;


Keep in mind that above code is neither the best practice or tested. I have written it right here right now. So no guarantees if it doesn't compiles. It is just to help you in the right direction.
 
Share this answer
 
Comments
Wendelius 24-May-12 15:01pm    
The idea should work just fine!
Sandeep Mewara 25-May-12 0:54am    
Good. 5!
VJ Reddy 25-May-12 6:20am    
Good answer. 5!
Hi,

You just need to do some string manipulation...
for that I've explained a little example.

C#
ArrayList List = new ArrayList();
            string TstStr = "1,dt3435,26/5/2012 ~ 2,dt738,27/5/2012 ~ 3,dt892,26/5/2012";
            string[] SplittedString = TstStr.Split('~');
            foreach (string str in SplittedString)
            {
                string ConcatStr = "";
                string[] SubSplit = str.Trim().Split(',');
                foreach (string s in SubSplit)
                {
                    ConcatStr += s + " ";
                }
                List.Add(ConcatStr);
            }

            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("data", typeof(string));                        
            dt.Columns.Add(dc);
            for (int i = 0; i < List.Count;i++ )
            {
                dt.Rows.Add();
                dt.Rows[i][0] = List[i];
            }
            dataGridView1.DataSource = dt;



Hope this help you:)

Happy Coding :)
 
Share this answer
 
Comments
Maciej Los 24-May-12 11:58am    
Good work, +5!
Thiện Lê 4-Sep-18 22:44pm    
thanks very much
And on variation could be to use LINQ. You could use a query like following:
C#
string s1 = "1,dt3435,26/5/2012 ~ 2,dt738,27/5/2012 ~ 3,dt892,26/5/2012";

var result = from item3 in
                (from item2 in
                    (from item1 in s1.Split('~') 
                     select item1)
                 select item2.Split(','))
             select new {
                field1 = item3[0],
                field2 = item3[1],
                field3 = item3[2]
             };

Of course it would be necessary to use a real type instead of anonymous type (...select new MyType {...) but that's an example if you want to test if it is feasible for you (run it using a debugger and see what's the result in result :)).
 
Share this answer
 
Comments
VJ Reddy 23-May-12 21:48pm    
Good answer. 5!
Wendelius 24-May-12 15:00pm    
Thanks :)
Maciej Los 24-May-12 11:58am    
Good work, +5!
Sunny_Kumar_ 24-May-12 13:36pm    
thanks :)
Wendelius 24-May-12 15:00pm    
Thanks :)
The solutions already given are good.
As seen from the question, since it is required to bind the data generated to a grid with each data item into a column, then I think a DataTable with appropriate type for the Columns can be generated as follows:

C#
string s1 = "1,dt3435,26/5/2012 ~ 2,dt738,27/5/2012 ~ 3,dt892,26/5/2012";

DataTable values = new DataTable();
values.Columns.Add("F1",typeof(int),null);
values.Columns.Add("F2",typeof(string),null);
values.Columns.Add("F3",typeof(DateTime),null);

values = s1.Split('~').Select( s => {
            var fields = s.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
            var row = values.NewRow();
            if (fields.Length == 3){
                row[0]=int.Parse(fields[0].Trim());
                row[1]=fields[1].Trim();
                row[2] = DateTime.ParseExact(fields[2].Trim(),"d/M/yyyy",
                        System.Globalization.CultureInfo.InvariantCulture);
            }
            return row;
        }).CopyToDataTable();

//Content of values table
//F1   F2            F3
//1 dt3435 5/26/2012 12:00:00 AM
//2 dt738 5/27/2012 12:00:00 AM
//3 dt892 5/26/2012 12:00:00 AM
 
Share this answer
 
Comments
Maciej Los 24-May-12 11:59am    
Good work, +5!
VJ Reddy 25-May-12 6:17am    
Thank you, losmac :)
Wendelius 24-May-12 15:00pm    
Nice approach!
VJ Reddy 25-May-12 6:18am    
Thank you, Mika :)
Sandeep Mewara 25-May-12 0:54am    
Yep, agree with Mika. 5!
Here is another option that uses Linq and a class:

C#
string str = "1,dt3435,26/5/2012 ~ 2,dt738,27/5/2012 ~ 3,dt892,26/5/2012";
    var result = str.Split('~').Select(i => new test(i));

}

class test
{
    public int Index
    {
        get { return _index; }
        set { _index = value; }
    }
    private int _index;

    public string Name { get; set; }

    private DateTime _date;

    public DateTime Date
    {
        get { return _date; }
        set { _date = value; }
    }

    public test(string input)
    {
        var parts = input.Split(',');
        int.TryParse(parts[0].Trim(), out _index);
        Name = parts[1].Trim();
        DateTime.TryParse(parts[2].Trim(), out _date);
    }
}


I like strong typing for something like this. Unfortunately cannot use properties in out arguments, which I think is kind of stupid.
 
Share this answer
 
v2
Comments
Sandeep Mewara 25-May-12 0:55am    
Yep, 5!
Here is another solution.. The end result is a list with all the values .. This code is running.. you could run and see..

C#
public class resultObj
{
    public string Val1 { get; set; }
    public string Val2 { get; set; }
    public string Val3 { get; set; }

}

   string tempstr = "1,dt3435,26/5/2012 ~ 2,dt738,27/5/2012 ~ 3,dt892,26/5/2012";

        string[] words = tempstr.Split('~');
        List<resultObj> lstResults = new List<resultObj>();

        foreach (string tstr in words)
        {
            string[] interStr = tstr.Split(',');

            resultObj rObj = new resultObj();
            rObj.Val1 = interStr[0];
            rObj.Val2 = interStr[1];
            rObj.Val3 = interStr[2];
            lstResults.Add(rObj);
           
        }

        grid.Datasource = lstResults;
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900