2
Reply

How can I add arraylist items in Datatable?

Mani Kandan

Mani Kandan

Sep 14 2016 12:25 PM
228
Hello Everyone,
 
How can I add arraylist items in Datatable? I can create a program but, it occured errors.
Kindly answer me with your explanation.
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Data;  
  7. using System.Web;  
  8. using System.Collections;  
  9.   
  10.   
  11. namespace ConsoleApplication14  
  12. {  
  13.     class Program  
  14.     {  
  15.         static void Main(string[] args)  
  16.         {  
  17.             DataTable table1 = new DataTable();  
  18.             table1.Columns.Add("M1");  
  19.             table1.Columns.Add("M2");  
  20.             table1.Columns.Add("M3");  
  21.             DataRow row1 = table1.NewRow();  
  22.             row1["M1"] = "100";  
  23.             row1["M2"] = "120";  
  24.             row1["M3"] = "322";  
  25.             table1.Rows.Add(row1);  
  26.             DataRow row2 = table1.NewRow();  
  27.             row2["M1"] = "200";  
  28.             row2["M2"] = "350";  
  29.             row2["M3"] = "542";  
  30.             table1.Rows.Add(row2);  
  31.             DataRow row3 = table1.NewRow();  
  32.             row3["M1"] = "300";  
  33.             row3["M2"] = "120";  
  34.             row3["M3"] = "544";  
  35.             table1.Rows.Add(row3);  
  36.   
  37.             table1.Columns.Add("ID"typeof(int));  
  38.             table1.Columns.Add("Name_student"typeof(String));  
  39.   
  40.             DataView view = new System.Data.DataView(table1);  
  41.             DataTable selected = new DataTable();  
  42.   
  43.             selected = view.ToTable(true"ID""Name_student""M1""M2""M3");  
  44.   
  45.             string Name_student = string.Empty;  
  46.             int i = 0;  
  47.   
  48.             ArrayList arrlist = new ArrayList();  
  49.             arrlist.Add("Mohammad");  
  50.             arrlist.Add("Ali");  
  51.             arrlist.Add("Fahe");  
  52.   
  53.             foreach (DataRow item in table1.Rows)  
  54.             {  
  55.                 foreach (DataRow row in selected.Rows)  
  56.                 {  
  57.                     row["Name_student"] = arrlist[i];  
  58.                     i = i + 1;  
  59.                     for (int j = 0; j <= 3; j++)  
  60.                     {  
  61.                         row["ID"] = j;  
  62.                     }  
  63.                 }  
  64.             }  
  65.             var objselelcted = selected;  
  66.         }  
  67.     }  
  68. }  
 

Answers (2)