Insert a new row into DataTable

后端 未结 4 423
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-13 17:12

I have a datatable filled with staff data like..

Staff 1 - Day 1 - Total
Staff 1 - Day 2 - Total
Staff 1 - Day 3 - Total
Staff 2 - Day 1 - Total
Staff 2 - Da         


        
相关标签:
4条回答
  • 2020-12-13 17:29
    // create table
    var dt = new System.Data.DataTable("tableName");
    
    // create fields
    dt.Columns.Add("field1", typeof(int));
    dt.Columns.Add("field2", typeof(string));
    dt.Columns.Add("field3", typeof(DateTime));
    
    // insert row values
    dt.Rows.Add(new Object[]{
                    123456,
                    "test",
                    DateTime.Now
               });
    
    0 讨论(0)
  • 2020-12-13 17:37
    // get the data table
    DataTable dt = ...;
    
    // generate the data you want to insert
    DataRow toInsert = dt.NewRow();
    
    // insert in the desired place
    dt.Rows.InsertAt(toInsert, index);
    
    0 讨论(0)
  • 2020-12-13 17:49

    You can do this, I am using

    DataTable 1.10.5

    using this code:

    var versionNo = $.fn.dataTable.version;
    alert(versionNo);
    

    This is how I insert new record on my DataTable using row.add (My table has 10 columns), which can also includes HTML tag elements:

    function fncInsertNew() {
                var table = $('#tblRecord').DataTable();
    
                table.row.add([
                        "Tiger Nixon",
                        "System Architect",
                        "$3,120",
                        "2011/04/25",
                        "Edinburgh",
                        "5421",
                        "Tiger Nixon",
                        "System Architect",
                        "$3,120",
                        "<p>Hello</p>"
                ]).draw();
            }
    

    For multiple inserts at the same time, use rows.add instead:

    var table = $('#tblRecord').DataTable();
    
    table.rows.add( [ {
            "Tiger Nixon",
            "System Architect",
            "$3,120",
            "2011/04/25",
            "Edinburgh",
            "5421"
        }, {
            "Garrett Winters",
            "Director",
            "$5,300",
            "2011/07/25",
            "Edinburgh",
            "8422"
        }]).draw();
    
    0 讨论(0)
  • 2020-12-13 17:51

    @William You can use NewRow method of the datatable to get a blank datarow and with the schema as that of the datatable. You can populate this datarow and then add the row to the datatable using .Rows.Add(DataRow) OR .Rows.InsertAt(DataRow, Position). The following is a stub code which you can modify as per your convenience.

    //Creating dummy datatable for testing
    DataTable dt = new DataTable();
    DataColumn dc = new DataColumn("col1", typeof(String));
    dt.Columns.Add(dc);
    
    dc = new DataColumn("col2", typeof(String));
    dt.Columns.Add(dc);
    
    dc = new DataColumn("col3", typeof(String));
    dt.Columns.Add(dc);
    
    dc = new DataColumn("col4", typeof(String));
    dt.Columns.Add(dc);
    
    DataRow dr = dt.NewRow();
    
    dr[0] = "coldata1";
    dr[1] = "coldata2";
    dr[2] = "coldata3";
    dr[3] = "coldata4";
    
    dt.Rows.Add(dr);//this will add the row at the end of the datatable
    //OR
    int yourPosition = 0;
    dt.Rows.InsertAt(dr, yourPosition);
    
    0 讨论(0)
提交回复
热议问题