Error is thrown when datatable is empty, due to the javascript function assigned to it.

Error is thrown when datatable is empty, due to the javascript function assigned to it.

AG94AG94 Posts: 1Questions: 1Answers: 0
edited December 2021 in DataTables

1) Javascript function for searching and sorting the gridview data.

<script type="text/javascript">        

        $(document).ready(function () {
                $('#ContentPlaceHolder1_GridView1').DataTable(
                    {
                        bLengthChange: true,
                        lengthMenu: [[10, 20, -1], [10, 20, "All"]],
                        bFilter: true,
                        bSort: true,
                        bPaginate: true
                    }
               );            
        });
    </script>

2) Code to bind the datatable to the gridview.

public void FillGrid_GridView1()
{   
   GridView1.DataSource = null;
   GridView1.DataBind();

   DataTable dt = new DataTable();

   if (dt.Rows.Count > 0)
    {         
         GridView1.DataSource = dt;                    
         GridView1.DataBind();

         GridView1.UseAccessibleHeader = true;
         GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
}

I have a gridview, namely GridView1, to which javascript is applied for searching and sorting the data. When the datatable being assigned to the gridview is empty, the error thrown is :
" Uncaught TypeError: Cannot read properties of undefined (reading 'mData'). "

I’d be grateful if someone provides a solution for solving this error.

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    You normally get those kind of errors if the number of columns in the data isn't consistent. If you can link to your page, we can take a look,

    Colin

Sign In or Register to comment.