Click here to Skip to main content
15,885,032 members
Articles / Web Development / ASP.NET
Article

Userful GridView ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.88/5 (12 votes)
31 Jan 2012CPOL 75.5K   3.7K   51   16
I tried to summrize the common feature which we generally want in asp.net GridView.
gv.JPGImage 2

Introduction

I tried to summrize the common feature which we generally want in asp.net GridView.

  1. Fixed hedder and scrollbar – using jQuery
  2. Check Uncheck all rows - using jQuery
  3. Sorting direction imgae indicator - just by code behind no CSS no Script
  4. Filter GridView row at client side - javascript

Using the code

This is just a code snips so it will not run at you end you need to include this codes to your project and probably need to make necessarily changes.<o:p>

You need to include the jquery-1.4.4.min.js and ScrollableGridPlugin.js" in you prject. (I am uploding that with this article)

1. Fixed hedder and scrollbar – using jQuery

<o:p>gv_scroll.JPG

gv_scroll.JPG

Script:
JavaScript
<script src="../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
    <script src="../Scripts/ScrollableGridPlugin.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">

       // Check/ Uncheck all
        $(window).bind('load', function () {
            var headerChk = $(".chkHeader input");
            var itemChk = $(".chkItem input");
            headerChk.bind("click", function () {
                itemChk.each(function () { this.checked = headerChk[0].checked; })
            }
                   );
            itemChk.bind("click", function () { if ($(this).checked == false) headerChk[0].checked = false; });
        });


        // Fixing the hear for Scroll
       $(document).ready(function () {
            //Invoke Scrollable function.
            $('#<%=SearchResult.ClientID %>').Scrollable({
                ScrollHeight: 600,        
            });
        });


         //GV runtime filter
        function filter (term, GvId,SearchOn)
        {
            var SearchOn ='#'+SearchOn;
            var cellNr = $(SearchOn).find('input:checked').val();
            //alert(term);
            var suche = term.value.toLowerCase();
            var table = document.getElementById(GvId);
            var ele;
            for (var r = 0; r < table.rows.length; r++)
            {
                ele = table.rows[r].cells[cellNr].innerHTML.replace(/<[^>]+>/g,"");
                if (ele.toLowerCase().indexOf(suche)>=0 )
                    table.rows[r].style.display = '';
                else table.rows[r].style.display = 'none';
            }

         }

    </script>          
Code Behind: 
protected void Page_Load(object sender, EventArgs e)
    {

        try
        {
            if (!IsPostBack)
            {
                ViewState[FixInfo.SortColum] = " ";
                ViewState[FixInfo.SortDirection] = " ";
                GetPendingAds();

                //for GV runtime filter
                SearchTxt.Attributes.Add("onkeyup", "filter(this,'" + SearchResult.ClientID + "','" 
                    + SearchOn.ClientID + "')");

            }
        }
        catch (Exception ex)
        {
            LocalHelper.ShowError(ex.Message);
        }

    }

    protected void SearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            foreach (TableCell tc in e.Row.Cells)
            {
                if (ViewState[FixInfo.SortDirection].ToString().Length > 2)
                {
                    try
                    {
                        LinkButton lnkBtn = (LinkButton)tc.Controls[0];

                        if (lnkBtn != null)
                            if (ViewState[FixInfo.SortColum].ToString().Equals(lnkBtn.CommandArgument, StringComparison.InvariantCultureIgnoreCase))
                            {
                                System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();

                                img.ImageUrl = "~/Images/" + ViewState[FixInfo.SortDirection].ToString() + ".gif";
                                tc.Controls.Add(img);
                            }
                    }
                    catch
                    {
                    }

                }

            }
        }
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Sapient Global Market
United States United States
Himanshu Thawait is Associate Arch at Sapient Global Markets.

He is expert in developing EAI, BizTalk with EDI(HIPPA)., Web applications, Micro web services, Angular, ASP.NET MVC, C#, VB.NE T, VB 6, SQL Server, Oracle, No SQL, Classic ASP, XML and JavaScript, IBM MQSC, IBM DB2.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey30-Jun-14 1:33
professionalManoj Kumar Choubey30-Jun-14 1:33 
QuestionRequest for entire code Pin
Bridewin17-Apr-13 2:54
Bridewin17-Apr-13 2:54 
AnswerRe: Request for entire code Pin
Himanshu Thawait17-Apr-13 4:53
Himanshu Thawait17-Apr-13 4:53 
GeneralMy vote of 1 Pin
Member 948470624-Dec-12 22:55
Member 948470624-Dec-12 22:55 
GeneralRe: My vote of 1 Pin
Himanshu Thawait27-Dec-12 6:06
Himanshu Thawait27-Dec-12 6:06 
Questiongood move Pin
Mr. Tapan Kumar10-Oct-12 9:56
Mr. Tapan Kumar10-Oct-12 9:56 
GeneralMy vote of 2 Pin
Mr. Tapan Kumar10-Oct-12 9:55
Mr. Tapan Kumar10-Oct-12 9:55 
GeneralMy vote of 5 Pin
CS140111-Mar-12 18:18
CS140111-Mar-12 18:18 
Questiondisplay mysqldata on gridview Pin
dcba129-Feb-12 22:19
dcba129-Feb-12 22:19 
QuestionMissing functions Pin
Paul M Cohen7-Feb-12 9:25
Paul M Cohen7-Feb-12 9:25 
AnswerRe: Missing functions Pin
Himanshu Thawait8-Feb-12 5:47
Himanshu Thawait8-Feb-12 5:47 
GeneralRe: Missing functions Pin
Paul M Cohen8-Feb-12 9:04
Paul M Cohen8-Feb-12 9:04 
GeneralRe: Missing functions Pin
Himanshu Thawait8-Feb-12 11:05
Himanshu Thawait8-Feb-12 11:05 
GeneralRe: Missing functions Pin
Paul M Cohen8-Feb-12 16:52
Paul M Cohen8-Feb-12 16:52 
I am getting a syntax error after "ScrollHeight: 600," and the grid does not show scroll bars but there is something invisible on the right edge of the grid when it should be scrolling. It looks like maybe the scroll bar is not visible. I am using the latest version of JQuery 1.7.1 if that matters.
SuggestionExt.net Pin
Emad Mokhtar1-Feb-12 12:07
Emad Mokhtar1-Feb-12 12:07 
GeneralRe: Ext.net Pin
Jason Vogel1-Feb-12 16:37
Jason Vogel1-Feb-12 16:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.