locked
DataTable To Array RRS feed

  • Question

  • User1944504294 posted

    Hi,

    How to store the datas from the  datatable that has threecolumns to array and store that array into the viewstate.

    Best regards,
    Renuka.V

    Monday, June 16, 2014 10:18 AM

Answers

  • User103196646 posted

    Hello!

    Here's how to convert the datatable to an object list and then store the object list in view state. Object Lists are easier to wrok with than array lists.

    public Class Customer
    {
     public string Name { get; set; }
     public int ID { get; set; }
     public string Title { get; set; }
    }
    
    private List<Customer> GetCustomerList(DataTable dt)
        {
    
            var convertedList = (from rw in dt.AsEnumerable()
                       select new Customer()
                       {
                           ID = Convert.ToInt32(rw["ID"]),
                           Name = Convert.ToString(rw["Name"]),
                           Title = Convert.ToString(rw["Title"]),
                       }).ToList();
    
            return convertedList;
        }
    
    // Save object list to ViewState
    ViewState[lstCustomer] = new GetCustomerList(dt);

    Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 16, 2014 10:39 AM
  • User724169276 posted

    Hello Renuka,

    You can try this implementation:

    public ArrayList ConvertDTToArray(ref DataTable dt)
    {
    	ArrayList converted = new ArrayList(dt.Rows.Count);
    	foreach (DataRow row in dt.Rows)
    	{
    		converted.Add(row);
    	}
    	return converted;
    }
    

    And you can call this method like below:

    ArrayList arr = ConvertDTToArray(ref dt);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 16, 2014 10:06 PM

All replies

  • User103196646 posted

    Hello!

    Here's how to convert the datatable to an object list and then store the object list in view state. Object Lists are easier to wrok with than array lists.

    public Class Customer
    {
     public string Name { get; set; }
     public int ID { get; set; }
     public string Title { get; set; }
    }
    
    private List<Customer> GetCustomerList(DataTable dt)
        {
    
            var convertedList = (from rw in dt.AsEnumerable()
                       select new Customer()
                       {
                           ID = Convert.ToInt32(rw["ID"]),
                           Name = Convert.ToString(rw["Name"]),
                           Title = Convert.ToString(rw["Title"]),
                       }).ToList();
    
            return convertedList;
        }
    
    // Save object list to ViewState
    ViewState[lstCustomer] = new GetCustomerList(dt);

    Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 16, 2014 10:39 AM
  • User724169276 posted

    Hello Renuka,

    You can try this implementation:

    public ArrayList ConvertDTToArray(ref DataTable dt)
    {
    	ArrayList converted = new ArrayList(dt.Rows.Count);
    	foreach (DataRow row in dt.Rows)
    	{
    		converted.Add(row);
    	}
    	return converted;
    }
    

    And you can call this method like below:

    ArrayList arr = ConvertDTToArray(ref dt);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 16, 2014 10:06 PM