web-dev-qa-db-fra.com

Insérer une nouvelle ligne dans DataTable

J'ai un datatable rempli avec les données du personnel comme ..

Staff 1 - Day 1 - Total
Staff 1 - Day 2 - Total
Staff 1 - Day 3 - Total
Staff 2 - Day 1 - Total
Staff 2 - Day 2 - Total
Staff 2 - Day 3 - Total
Staff 2 - Day 4 - Total

Je veux modifier pour que le résultat soit comme ca.

Staff 1 - Day 1 - Total
Staff 1 - Day 2 - Total
Staff 1 - Day 3 - Total
Total   -       - Total Value
Staff 2 - Day 1 - Total
Staff 2 - Day 2 - Total
Staff 2 - Day 3 - Total
Staff 2 - Day 4 - Total
Total   -       - Total Value

pour conclure, je dois insérer le nombre total de lignes à la fin de chaque dossier du personnel.

Donc, ma question est de savoir comment insérer une ligne dans un datatable? Tkz ..

41
william
// 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);
59
Timwi

@William Vous pouvez utiliser la méthode NewRow du datatable pour obtenir un datarow vide et avec le schéma comme celui du datatable. Vous pouvez renseigner ce flux de données, puis ajouter la ligne à l'élément de données à l'aide de .Rows.Add(DataRow) OR .Rows.InsertAt(DataRow, Position). Ce qui suit est un code de talon que vous pouvez modifier à votre convenance.

//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);
81
samar
// 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
           });
19
gpaoli

Vous pouvez le faire, j'utilise 

DataTable 1.10.5

en utilisant ce code:

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

C’est ainsi que j’insère un nouvel enregistrement dans mon DataTable à l’aide de row.add (Ma table a 10 colonnes), qui peut également inclure des éléments de balise HTML:

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();
        }

Pour plusieurs insertions en même temps, utilisez rows.add à la place:

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
Willy David Jr