Questions And Answers

More Tutorials

How to create multi-column primary key for a DataTable in Javascript

The Javascript shown below is used to initialise the table shown in this example:

var editor; // use a global for the submit and return data rendering in the examples
$(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
        ajax: '../php/compoundKey.php',
        table: '#example',
        fields: [ {
                label: 'Staff member:',
                name: 'users_visits.user_id',
                type: 'select'
            }, {
                label: 'Visiting site:',
                name: 'users_visits.site_id',
                type: 'select'
            }, {
                label: 'Date:',
                name: 'users_visits.visit_date',
                type: 'datetime'
    } );
    $('#example').DataTable( {
        dom: 'Bfrtip',
        ajax: {
            url: '../php/compoundKey.php',
            type: 'POST'
        columns: [
            { data: null, render: function ( data, type, row ) {
                return data.users.first_name+' '+data.users.last_name;
            } },
            { data: '' },
            { data: 'users_visits.visit_date' }
        select: true,
        buttons: [
            { extend: 'create', editor: editor },
            { extend: 'edit',   editor: editor },
            { extend: 'remove', editor: editor }
    } );
} );

Create multi column PrimaryKey for a DataTable

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html>
<script runat="server">
    void Button1_Click(object sender, System.EventArgs e)
        DataTable dt = new DataTable();
        dt.TableName = "Books";

        DataColumn dc1 = new DataColumn();
        dc1.ColumnName = "BookID";
        dc1.DataType = typeof(int);

        DataColumn dc2 = new DataColumn();
        dc2.ColumnName = "BookName";
        dc2.DataType = typeof(string);

        //this line set the 'BookID' and 'BookName' columns to DataTable PrimaryKey
        dt.PrimaryKey = new DataColumn[] { dc1,dc2 };
        DataColumn dc3 = new DataColumn();
        dc3.ColumnName = "Author";
        dc3.DataType = typeof(string);

        dt.Rows.Add(new object[] { 1, "Canvas Pocket Reference", "David Flanagan" });
        dt.Rows.Add(new object[] { 2, "Google Analytics", "Justin Cutroni" });
        dt.Rows.Add(new object[] { 3, " example ebook", "test author" });

            Uncomment this line to get an error because 'BookID' and 'BookName'
            columns are PrimaryKey. So both columns make the row unique.
        //dt.Rows.Add(new object[] { 3, " example ebook", "Jenny Jones" });

        GridView1.DataSource = dt;

<html xmlns="">
<head id="Head1" runat="server">
    <title>How to create multi column PrimaryKey for a DataTable in</title>
    <form id="form1" runat="server">
        <h2 style="color:DarkBlue; font-style:italic;">
            How to create multi column
            <br /> PrimaryKey for a DataTable in
        <hr width="375" align="left" color="CornFlowerBlue" />
            <HeaderStyle BackColor="DodgerBlue" Height="35" />
            <RowStyle BackColor="OrangeRed" />
            <AlternatingRowStyle BackColor="Tomato" />
        <br />
            Text="Populate GridView"


In this page (written and validated by ) you learned about . What's Next? If you are interested in completing Javascript tutorial, we encourage you simply to start here: Javascript Tutorial.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.