Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Cannot read property 'mData' of undefined for JQuery DataTable

I have bound my datatable to my gridview in ASP.Net. I Have tried to use jQuery dataTable to show search and sorting options in my gridview, but the datatable throws me an error.

The Error is: Uncaught TypeError: Cannot read property 'mData' of undefined" this is the error.

I have used the below code:

function pageLoad(sender, args) {
    $('#grdProducts').DataTable(
        $(document).ready(function () {
            $('.dataTable').dataTable();
        })
    );
}
<asp:GridView ID="grdProducts" runat="server" AutoGenerateColumns="false" CssClass="table table-hover dataTable">
    <Columns>
         <asp:TemplateField Visible="false">
            <ItemTemplate>
                <asp:Label ID="lblProductID" runat="server"                        Text='<%# Eval("ProductID")%>' />
            </ItemTemplate>
        </asp:TemplateField>
        <%--<asp:BoundField DataField="GroupName" HeaderText="Group Name" />--%>
        <asp:BoundField DataField="BrandID" HeaderText="Brand" />
        <asp:BoundField DataField="ProductName" HeaderText="Product" />
        <asp:BoundField DataField="ShortCode" HeaderText="Code " />
        <asp:BoundField DataField="Price" HeaderText="Price" />
        <asp:TemplateField HeaderText="Update">
            <ItemTemplate>
                <asp:Button ID="btnUpdate" runat="server" Text="Update" class="btn btn-info" OnClick="btnUpdate_OnClick" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
like image 363
Atibur Rahman Avatar asked Jan 06 '16 09:01

Atibur Rahman


People also ask

What is mData in DataTable?

mData can be given in a number of different ways which effect its behaviour: integer - treated as an array index for the data source. This is the default that DataTables uses (incrementally increased for each column). string - read an object property from the data source.

What does Cannot read property of undefined mean?

What Causes TypeError: Cannot Read Property of Undefined. Undefined means that a variable has been declared but has not been assigned a value. In JavaScript, properties and functions can only belong to objects.

How add Colspan to DataTable?

COLSPAN: Ajax or JavaScript sourced data For example: var table = $('#example'). DataTable({ ajax: 'https://api.myjson.com/bins/qgcu', createdRow: function(row, data, dataIndex){ // If name is "Ashton Cox" if(data[0] === 'Ashton Cox'){ // Add COLSPAN attribute $('td:eq(1)', row).


1 Answers

By default ASP GridView keeps everything under tag and will not render grid header under , which will result in 'mData' error.

In order to do so, use below line after binding data to GridView:

GridViewName.HeaderRow.TableSection = TableRowSection.TableHeader;
like image 145
Kuldeep Gupta Avatar answered Oct 18 '22 23:10

Kuldeep Gupta