Sunday, October 3, 2010

Simplest way to use JQuery DataTable in (C#)

Hi All,

Today i will be using JQuery dataTable in c# code without any 'bProcessing' , 'bServerSide' and 'sAjaxSource'.

First of All what is Jquery Datatable, just Click and find out :).

After getting the basic structure of JQuery datatable you may find that all the server side examples on site is in PHP and the stuff done for seems so difficult nor such newbies like me. so lets talk about a simpler solution.

First we create a simple HTML table. and the place where data will get populated just add an ASP Literal control. :)

<table class="datatable">
                                <th class="bSortable">
                                    <input type="checkbox" class="checkbox select-all" />
                                    First Name
                                    Middle Name
                                    Last Name
                            <asp:Literal runat="server" ID="ltData"></asp:Literal>

Now on the server side write a simple function to populate the literal with required data.

private void loadGrid()
        StringBuilder sb = new StringBuilder();
        DataTable dt = MyData.GetAllUserData();
        if (dt != null)
            if (dt.Rows.Count > 0)
                foreach (DataRow dr in dt.Rows)

                    sb.Append("<input type='checkbox' class='checkbox' />");


        ltData.Text = sb.ToString();


For initializing the data table you can refer to the site or use the following code and write it in document.ready() function of jQuery.

function LoadTable() {
  'bLengthChange': true,
  'bPaginate': true,
  'sPaginationType': 'full_numbers',
  'iDisplayLength': 5,
  'bInfo': false,
   'sSearch': 'Search all columns:',
    'sNext': '&gt;',
    'sLast': '&gt;&gt;',
    'sFirst': '&lt;&lt;',
    'sPrevious': '&lt;'

At this point we are done with the basic grid view with just text data but what if we need to perform some functions like delete a particular row, Just don't worry about it create a webservice and write the deletion function in that service then use the following code to call it using jQuery.

function DeleteMe(string IdToBeDeleted){
var webMethod = 'DeleteRecord.asmx/DeleteUser'
var parameters = "{'ID':'" + IdToBeDeleted + "'}"

        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {    
                alert('Success Deletion');
        error: function(e){
                 alert('Failure Deletion');                         

and for Datatable just create an other column and call this DeleteMe function with RowId as parameter to it and a nice deletion will occur.

Hope it helps.

Happy coding....


Salman ansari said...

thax a lot..but i want to perform sorting in my can u tell me how it possible using your code

Usman Shabbir said...

if you are loading the complete Dataset at one time then "bsorting" : "true" would do he job but if you need to do custom sorting from Database with custom pagination then find my posts

and this procedure of you are using mySQL at backend.

for SQL server you can find many procedures that can do custom sorting.

Hope it helps.

Anant said...

Hi, its a really good article.
You have shown how to delete.But what after I delete that row, I need to bind the table again from database.
How to hide the last deleted row?

Vara Reddy said...

Hi this may help some, this is having more options...

for Gridview

for repeater