How do we maintain next row of data column and save those report in particular path using c#

Gani_tpt 1,506 Reputation points
2020-12-01T16:15:11.47+00:00

I want to maintain employee report who have not been regularized in office in and out.

for that, we have to update empolyee reason row by row of every employee. one employee will have many reason.

for example,

String Reason1 = "Not Well";
String Reason2 = "Out of office";
String Reason3 = "Came to Late on time";
String Reason4 = "Came to Office but late";
String Reason4 = "Came to Office with permission";

foreach (DataRow row in EmpReport.Rows) // Main Table
{

DataRow dr = EmpReasonReport.NewRow(); // New Data table ==> EmpReasonReport
ErrReport.Rows.Add(dr);
//New table create and update emp reason details

-------------------

-------------------

------------------

}

the final output would come in data table in the the below format.

44088-emp-reason-data.jpg

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,205 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Timon Yang-MSFT 9,571 Reputation points
    2020-12-03T06:35:04.823+00:00

    If you do not need to display it in the DataGridView, but need to put it in excel, please refer to this method:

            private void Form1_Load(object sender, EventArgs e)  
            {  
                System.Data.DataTable dataTable = new System.Data.DataTable();  
                dataTable.Columns.Add("EmpNo", typeof(int));  
                dataTable.Columns.Add("EmpName", typeof(string));  
                dataTable.Columns.Add("Reasons", typeof(MyList<string>));  
      
                dataTable.Rows.Add(1, "Timon", new MyList<string>() { "late", "reason2" });  
                dataTable.Rows.Add(2, "Timon1", new MyList<string>() { "late", "reason2", "reason3" });  
                dataTable.Rows.Add(3, "Timon2", new MyList<string>() { "late", "reason2" });  
      
                ExportDataTableToExcel(dataTable, @"d:\test\excel\tet.xlsx");  
            }  
            public  bool ExportDataTableToExcel(System.Data.DataTable dt, string filepath)  
            {  
                Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application(); ;  
                Workbook oWB = oXL.Workbooks.Add(Missing.Value); ;  
                Worksheet oSheet;  
                try  
                {  
                    oXL.Visible = true;  
                    oXL.DisplayAlerts = false;  
      
                    oSheet = (Worksheet)oWB.ActiveSheet;  
                    oSheet.Name = "Data";  
      
                    int rowCount = 1;  
                    foreach (DataRow dr in dt.Rows)  
                    {  
                        rowCount += 1;  
                        for (int i = 1; i < dt.Columns.Count + 1; i++)  
                        {  
                            // Add the header the first time through   
                            if (rowCount == 2)  
                            {  
                                oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;  
                            }  
                            oSheet.Cells[rowCount, i] = dr[i - 1].ToString();  
                        }  
                    }  
                    oWB.SaveAs(filepath);  
                }  
                catch (Exception ex)  
                {  
                    Console.WriteLine(ex.Message);  
                }  
                finally  
                {  
                    oWB.Close();  
                    oXL.Quit();  
                }  
                return true;  
            }  
    

    44608-2.png

    Does this meet your requirements?


    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.

    1 person found this answer helpful.

  2. Timon Yang-MSFT 9,571 Reputation points
    2020-12-02T09:33:26.773+00:00

    If you want such a structure, the type of a column in the datatable needs to be a collection.

    Are you using WinForm? If so, in order to output such a datatable in the datagridview, you need to customize a DataGridViewControlColumn so that the collection can be displayed in the datagridview.

    This is an example, he defines a DataGridViewRichTextBoxColumn.

    RichTextBox Cell in a DataGridView

    This is how I use the code:

        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
            }  
      
            private void Form1_Load(object sender, EventArgs e)  
            {  
                dataGridView1.RowTemplate.Height = 50;  
                DataTable dataTable = new DataTable();  
                dataTable.Columns.Add("EmpNo", typeof(int));  
                dataTable.Columns.Add("EmpName", typeof(string));  
                dataTable.Columns.Add("Reasons", typeof(MyList<string>));  
      
                dataTable.Rows.Add(1, "Timon", new MyList<string>() { "late", "reason2" });  
                dataTable.Rows.Add(2, "Timon1", new MyList<string>() { "late", "reason2","reason3" });  
                dataTable.Rows.Add(3, "Timon2", new MyList<string>() { "late", "reason2" });  
                dataGridView1.DataSource = dataTable;  
      
                DataGridViewRichTextBoxColumn dataGridViewRichTextBoxColumn = new DataGridViewRichTextBoxColumn();  
                dataGridViewRichTextBoxColumn.HeaderText = "Reasons";  
                dataGridViewRichTextBoxColumn.DataPropertyName = "Reasons";  
                dataGridView1.Columns.Insert(dataGridView1.Columns.Count, dataGridViewRichTextBoxColumn);  
            }  
        }  
        //If I don’t override, the content displayed in the RichTextBox will be “System.Collections.Generic.List`1[System.String]”  
        public class MyList<T> : List<T>   
        {  
            public override string ToString()  
            {  
                StringBuilder stringBuilder = new StringBuilder();  
                foreach (var item in this)  
                {  
                    stringBuilder.Append(item + Environment.NewLine);  
                }  
                return stringBuilder.ToString();  
            }  
        }  
    

    Result:
    44374-1.png


    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.