How do we handle Data Row and its column value in another procedure class file from data table using c#

Gani_tpt 2,026 Reputation points
2020-12-01T15:55:43.07+00:00

I have employee data table.

In that, every row contains more than 100 columns.

It's very difficult to handle the same main page all the columns of every row.

In that case, Hoe do we pass all column values for processing in separate procedure and and update the same row in main table of main file.

for example,

foreach (DataRow DR in dtEmp.Rows)
{
if (!string.IsNullOrEmpty(ColName))
{
DR[ColName] = Empno;
DR[ColName] = Empname;
DR[ColName] = Salary;
DR[ColName] = PF;
DR[ColName] = Address1;
DR[ColName] = Address2;
DR[ColName] = country;
DR[ColName] = Gender;
DR[ColName] = phone;
--------
--------
--------
}

}

The above Dr[ColName] should handle in separate procedure of every row and finally it should update the same table of the corresponding cells.

How do we do this...?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,458 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,621 Reputation points
    2020-12-03T06:38:34.377+00:00

    Hi GaniCHN,
    You can add to your code to modify table method. The code is order to add a new DataRow to DataTable with Corresponding value.
    Here is a test code:

    public static void ModifyTable(DataTable dataTable)  
    {  
        DataRow DR = dataTable.NewRow();  
        dataTable.Rows.Add(DR);  
        DR["Id"] = 1;  
        DR["FirstName"] = "Empname";  
        DR["LastName"] = "Salary";         
    }  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,286 Reputation points
    2020-12-01T18:26:08.213+00:00

    Hello @GaniTPT

    Would the following mockup help out? If not tell me why.

    StartMethod is the starting point.

    using System;  
    using System.Data;  
    using System.Linq;  
      
    namespace YourNamespaceGoesHere.Classes  
    {  
        public class Operations  
        {  
            public static void StartMethod()  
            {  
                var dataTable = new DataTable();  
      
                dataTable.Columns.Add(new DataColumn() {ColumnName = "Id", DataType = typeof(int),  
                    AutoIncrement = true, AutoIncrementSeed = 1});  
      
                dataTable.Columns.Add(new DataColumn() {ColumnName = "FirstName", DataType = typeof(string)});  
                dataTable.Columns.Add(new DataColumn() {ColumnName = "LastName", DataType = typeof(string)});  
      
                dataTable.Rows.Add(null, "Karen", "Payne");  
                dataTable.Rows.Add(null, "Bob", "Adams");  
                dataTable.Rows.Add(null, "mary", "Jones");  
      
                ModifyTable(dataTable);  
      
                ModifyRow(dataTable.Rows.Cast<DataRow>().FirstOrDefault());  
      
                Console.WriteLine();  
      
            }  
            /// <summary>  
            /// Mockup for changing a field value for last name  
            /// </summary>  
            /// <param name="dataRow">Populated DataRow</param>  
            public static void ModifyRow(DataRow dataRow)  
            {  
                dataRow.SetField("LastName", dataRow.Field<string>("LastName") + " Changed");  
            }  
            //  
              
            /// <summary>  
            /// Modify all first names  
            /// </summary>  
            /// <param name="dataTable">Populated DataTable</param>  
            public static void ModifyTable(DataTable dataTable)  
            {  
                for (int index = 0; index < dataTable.Rows.Count; index++)  
                {  
                    dataTable.Rows[index]  
                        .SetField("FirstName",   
                            dataTable.Rows[index].Field<string>("FirstName")  
                                .ToUpper());  
                }  
            }  
        }  
    }  
    

    Here is a mockup for adding a new record where values are hard coded.

    public class Operations  
    {  
     public static void StartMethod()  
     {  
     var dataTable = new DataTable();  
      
     dataTable.Columns.Add(new DataColumn() {ColumnName = "Id", DataType = typeof(int),  
     AutoIncrement = true, AutoIncrementSeed = 1});  
      
     dataTable.Columns.Add(new DataColumn() {ColumnName = "FirstName", DataType = typeof(string)});  
     dataTable.Columns.Add(new DataColumn() {ColumnName = "LastName", DataType = typeof(string)});  
     dataTable.Columns.Add(new DataColumn() {ColumnName = "Salary", DataType = typeof(decimal)});  
      
     dataTable.Rows.Add(null, "Karen", "Payne", 123.4);  
     dataTable.Rows.Add(null, "Bob", "Adams", 456.0);  
     dataTable.Rows.Add(null, "mary", "Jones", 7779.0);  
     AddRecord(dataTable);  
     }  
     public static void AddRecord(DataTable dataTable)  
     {  
     var dr = dataTable.NewRow();  
     dataTable.Rows.Add(dr);  
      
     dr.SetField("FirstName","Mike");  
     dr.SetField("LastName","Lebow");  
     dr.SetField("Salary",45677);  
     Console.WriteLine();  
     }  
    }  
    

    44317-table.png