Skip to content
Loading
  • Rijwan Ansari
    +1
    Try this code
    1. string input = "1.0121,1.0131,1.0141,1.0151";    
    2. var columnValues = input.Split(',');    
    3.     
    4. for(int i=1; i<= columnValues.Length; i++){    
    5.     Console.Write ( "Col"+i+"       ");    
    6. }  
    7. Environment.NewLine;  
    8. for(int i=1; i<= columnValues.Length; i++){    
    9.     Console.Write ( columnValues[i]);    
    10. }  
    +1
  • Jignesh Kumar
    +1
    Hi Manish,
     
    You can use string split with delemeter, here you have comma ',' as deleneter so you can use that to split,
    1. var stringInput ="1.0121,1.0131,1.0141,1.0151";  
    2.   
    3. var stringAfterSplit = stringInput.Split(",");  
    4.   
    5. foreach (string col in stringAfterSplit )    
    6. Console.WriteLine(col);  
     
    +1
  • +1
    Hi,
     
    Try this code as per your requirements.
     
    aspx: 
     


      
    cs code: 
    1. protected void Page_Load(object sender, EventArgs e)  
    2.         {  
    3.             splitdata();  
    4.         }  
    5.   
    6.         private void splitdata()  
    7.         {  
    8.             string a = "1.0121,1.0131,1.0141,1.0151";  
    9.             var val = a.Split(',');  
    10.             List fields = new List(a.Split(new[] { ',' }));  
    11.   
    12.             for (int i=0; i < val.Length; i++)  
    13.             {  
    14.                 //grid1.DataSource = fields;//val                  
    15.                 //grid1.DataBind();  
    16.   
    17.   
    18.                 //create datatable and columns,  
    19.                 DataTable dtable = new DataTable();  
    20.                 dtable.Columns.Add(new DataColumn("Column 1"));  
    21.                 dtable.Columns.Add(new DataColumn("Column 2"));  
    22.                 dtable.Columns.Add(new DataColumn("Column 3"));  
    23.                 dtable.Columns.Add(new DataColumn("Column 4"));

    24.   
    25.                 //simple way create object for rowvalues here i have given only 3 add as per your requirement  
    26.                 object[] RowValues = {"", "", "" };  
    27.   
    28.                 //assign values into row object  
    29.                 RowValues[0] = val[0];  
    30.                 RowValues[1] = val[1];  
    31.                 RowValues[2] = val[2];  
    32.                 RowValues[1] = val[3];

    33.   
    34.                 //create new data row  
    35.                 DataRow dRow;  
    36.                 dRow = dtable.Rows.Add(RowValues);  
    37.                 dtable.AcceptChanges();  
    38.   
    39.                 //now bind datatable to gridview...   
    40.                 grid1.DataSource = dtable;  
    41.                 grid1.DataBind();  
    42.   
    43.             }  
    44.         }  
     Thanks
    +1