MOCKSTACKS
EN
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: 'sites.name' },
            { 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);
        dt.Columns.Add(dc1);

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

        //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.Columns.Add(dc3);

        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, "asp.net 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, "asp.net example ebook", "Jenny Jones" });

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to create multi column PrimaryKey for a DataTable in ado.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkBlue; font-style:italic;">
            How to create multi column
            <br /> PrimaryKey for a DataTable in ado.net
        </h2>
        <hr width="375" align="left" color="CornFlowerBlue" />
        <asp:GridView 
            ID="GridView1"
            runat="server"
            BorderColor="Snow"
            ForeColor="Snow"
            Width="550"
            >
            <HeaderStyle BackColor="DodgerBlue" Height="35" />
            <RowStyle BackColor="OrangeRed" />
            <AlternatingRowStyle BackColor="Tomato" />
        </asp:GridView>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Populate GridView"
            Height="45"
            Font-Bold="true"
            ForeColor="DarkBlue"
            />
    </div>
    </form>
</body>
</html>


Conclusion

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: mockstacks@gmail.com.


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 Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.