none
Adding sorted list to DataTable Row RRS feed

  • Question

  • I am trying to add IEnumerable items of string to a datatable row, but can't get it sorted. I want to get an output of 
     
    james,john
    Mary,Ann
    Edward,Anthony
     
    but I am getting  james,johnMary,AnnEdward,Anthony
    Below is my code. Any help will be appreciated.
     
     private void addToDataTable<T>(IEnumerable<T> rDataRow, string dataRowType)
            {
                try
                {
                    foreach (var r in rDataRow)
                    {
    				var type = r.GetType();
                     DataRow dr = reportData.NewRow();
    				 
    				  var ListItems = "";                            
                           
    
                            if (type.GetProperty("admin") != null)
                            {
                                foreach (var t in (IEnumerable)type.GetProperty("admin").GetValue(r, null))
                                {
    
                                    ListItems += t;
                                }
                                dr.SetField(dcAdmin, ListItems);
                            }
                            reportData.Rows.Add(dr);
    				}
    			}
    		}
    		


    Ebenezer

    Saturday, November 2, 2019 9:35 PM

Answers

  • I am not sure what you are asking but I will guess that you want to add a newline after each whatever. So you can use either one of the following.

    ListItems += t + Environment.NewLine;
    ListItems += t + "\r\n";




    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by denkyira Sunday, November 3, 2019 1:57 AM
    Saturday, November 2, 2019 9:50 PM

All replies

  • I am not sure what you are asking but I will guess that you want to add a newline after each whatever. So you can use either one of the following.

    ListItems += t + Environment.NewLine;
    ListItems += t + "\r\n";




    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by denkyira Sunday, November 3, 2019 1:57 AM
    Saturday, November 2, 2019 9:50 PM
  • Hi

    Thanks for your response. I did try ListItems += t + Environment.NewLine; but it didn't work. Let me try the second line and give feed back later


    Ebenezer

    Saturday, November 2, 2019 11:59 PM
  • Hello,

    I don't see any attempts at sorting and if that is part of the task unclear what the sort should be. 

    If you want to attempt items you could use a StringBuilder e.g. (think of this as conceptual as there is not DataRow items here).

    var mocked = new[] { "james,john", "Mary,Ann", "Edward,Anthony" };
    var sb = new StringBuilder();
    foreach (string item in mocked)
    {
        sb.AppendLine(item);
    }
    
    Console.WriteLine(sb.ToString());
    There are a handful of ways to sort but you need to be clear what you want other than what's shown as that is not sorted.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, November 3, 2019 1:54 AM
    Moderator