none
How to assign list value to data row field..? RRS feed

  • Question

  • How to assign list value to data row field..?

    DataRow DataReport = EmpReport.NewRow();

    List<string> EmpID = new List<string>();

    EmpID.Add("10001");

    EmpID.Add("20001");

    foreach (var row in rows)

    {

     DataReport["Empid"] = EmpID; // I want to assign all the Emp id into DataReport["Empid"] 

    }

    ???????


    • Edited by Gani tpt Friday, February 7, 2020 1:23 PM
    Friday, February 7, 2020 1:20 PM

Answers

  • Hi Gani,

    Thank you for posting here.

    You can use the following code to assign the list value to data row filed.

    static void Main(string[] args)
            {
                List<string> EmpID = new List<string>();
                EmpID.Add("10001");
                EmpID.Add("20001");
                EmpID.Add("30001");
                EmpID.Add("40001");
    
                DataTable table = new DataTable();
                table.Columns.Add("Id", typeof(string));
                table.Columns.Add("Name", typeof(string));
              
                for (int i = 0; i < EmpID.Count; i++)
                {
                    DataRow toInsert = table.NewRow();
                    table.Rows.Add(toInsert);
                }
                int number = 0;
                foreach (var item in EmpID)
                {
                    DataRow dr = table.Rows[number];
                    dr["Id"] = item;
                    number++;
                }
                Console.WriteLine();
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Gani tpt Monday, February 10, 2020 10:28 AM
    Monday, February 10, 2020 8:24 AM
    Moderator

All replies

  • Hello,

    This will set all identifiers to a field value as comma delimited.

    var empIdList = new List<string> {"10001", "20001"};
    
    foreach (DataRow row in EmpReport.Rows)
    {
        row.SetField("Empid", string.Join(",", empIdList.ToArray()));
    }

    Or with SetField specify the type

    var empIdList = new List<string> {"10001", "20001"};
    
    foreach (DataRow row in EmpReport.Rows)
    {
        row.SetField<string>("Empid", string.Join(",", empIdList.ToArray()));
    }


    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

    Friday, February 7, 2020 2:33 PM
    Moderator
  • It's just overwriting the existing values.

    But, i want add new fields in an array and finally i want to display all the array values into Data Row Field...

    ????

    Monday, February 10, 2020 6:16 AM
  • Hi Gani,

    Thank you for posting here.

    You can use the following code to assign the list value to data row filed.

    static void Main(string[] args)
            {
                List<string> EmpID = new List<string>();
                EmpID.Add("10001");
                EmpID.Add("20001");
                EmpID.Add("30001");
                EmpID.Add("40001");
    
                DataTable table = new DataTable();
                table.Columns.Add("Id", typeof(string));
                table.Columns.Add("Name", typeof(string));
              
                for (int i = 0; i < EmpID.Count; i++)
                {
                    DataRow toInsert = table.NewRow();
                    table.Rows.Add(toInsert);
                }
                int number = 0;
                foreach (var item in EmpID)
                {
                    DataRow dr = table.Rows[number];
                    dr["Id"] = item;
                    number++;
                }
                Console.WriteLine();
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Gani tpt Monday, February 10, 2020 10:28 AM
    Monday, February 10, 2020 8:24 AM
    Moderator
  • Thanks...done...
    Monday, February 10, 2020 10:29 AM