Extended the GridView implementation of a custom no-refreshing page, sorting, support for multiple data sources control TwfGridView, custom gridview

Source: Internet
Author: User

Extended the GridView implementation of a custom no-refreshing page, sorting, support for multiple data sources control TwfGridView, custom gridview

Recently, the View layer of the Project is becoming more and more non-refresh-like, especially for data presentation. It also has good support for Linq.
In the development of the WebFrom mode, the GridView is a very powerful and commonly used control, but it is not perfect, there is no built-in refresh and sorting (some people say that UpdatePanel or third-party plug-ins can achieve no refresh, but Haha... it is a heavyweight Implementation of no matter what you need. I believe many of my friends hate UpdatePanel,
Introduce a lot of long js libraries, and you will not feel the speed of refreshing them ), some data binding pages are also not supported (some people say that some data binding can also be implemented using third-party plug-ins such as aspNetPager, but can the plug-in handle paging and sorting in one event, ),
I also found a lot on the Internet, but it is not very easy to use, many do not provide source code, so I implemented
The specific functions are as follows:
1. Because it is extended to implement the self-contained GridView in. net, the functions of the GridView all have
2. Some data binding is supported to save server resources (that is, DataSource only needs to specify the data of the page to be displayed, and DataSource does not need to be the entire data source like the built-in GridView)
3. You can customize the pagination bar and pagination button styles without refreshing new pages.
4. You can customize the sorting bar and button styles without refreshing sorting.
5. Custom [ViewState] (1. Disable View state 2. Only maintain view State of Data 3. Maintain view State of all data and child controls)
6. Simplify event processing. Only one event is required for sorting and paging.

: Http://files.cnblogs.com/dint/TComponentsTest.rar

Binding different data sources:

DataTable:

        private void BindSource()        {            DataTable dtPersons = new DataTable();            dtPersons.Columns.Add("Name");            dtPersons.Columns.Add("Age");            dtPersons.Columns.Add("Address");            dtPersons.Columns.Add("Sex");            for (int i = 1, il = 5000; i <= il; i++)            {                DataRow nrow = dtPersons.NewRow();                nrow[0] = "Name" + i.ToString();                nrow[1] = "Age" + i.ToString();                nrow[2] = "Address" + i.ToString();                nrow[3] = "Sex" + i.ToString();                dtPersons.Rows.Add(nrow);            }            TwfGridView1.DataSource = dtPersons;            TwfGridView1.DataBind();        }

List, Linq:

        private void BindSource()        {            List<Person> lstPersons = new List<Person>();            for (int i = 1, il = 2000; i <= il; i++)            {                Person p = new Person()                {                    Id=i,                    Name = "Name" + i.ToString(),                    Age = "Age" + i.ToString(),                    Address = "Address" + i.ToString(),                    Sex = "Sex" + i.ToString()                };                lstPersons.Add(p);            }            TwfGridView1.DataSource = lstPersons;            TwfGridView1.DataBind();        }

Paging sorting events, some data sources (EntityFramework data operations, others are similar ):

 
// Some data sources need to be bound. The overloaded DataBind (int pageIndex, int allRecordCount) Method
Private void DataBinder (int pageindex, string sortexp, SortDirection? Direct) {using (var db = new EFDbContext (@ "Data Source = .; initial Catalog = dbtest; Integrated Security = true; ") {var results = db. persons. where (x => x. id <500); int rc = results. count (); int ps = (rc % TwfGridView1.PageSize = 0 )? Rc/TwfGridView1.PageSize: rc/TwfGridView1.PageSize + 1; if (pageindex> ps-1) pageindex = ps-1; var v = results; if (sortexp = "Id ") {if (direct = SortDirection. descending) {v = v. orderByDescending (x => x. id);} else {v = v. orderBy (x => x. id) ;}} else if (sortexp = "Name") {if (direct = SortDirection. descending) {v = v. orderByDescending (x => x. name);} else {v = v. orderBy (x => x. name) ;}} else if (sortexp = "Age") {if (direct = SortDirection. descending) {v = v. orderByDescending (x => x. age);} else {v = v. orderBy (x => x. age) ;}} else if (sortexp = "Sex") {if (direct = SortDirection. descending) {v = v. orderByDescending (x => x. sex);} else {v = v. orderBy (x => x. id) ;}} else if (sortexp = "Address") {if (direct = SortDirection. descending) {v = v. orderByDescending (x => x. address);} else {v = v. orderBy (x => x. address) ;}} else {v = v. orderBy (x => x. id);} v = v. skip (pageindex * TwfGridView1.PageSize ). take (TwfGridView1.PageSize); TwfGridView1.DataSource = v. toList (); TwfGridView1.DataBind (pageindex, rc); // The DataBind method is overloaded.} protected void TwfGridView1_PageSorting (object sender, GridViewPageEventArgs ePage, TwfComponents. EGridViewSortArgs eSort) {DataBinder (ePage. newPageIndex, eSort. sortExpression, eSort. sortDirection );}

  


The Gridview control is extended. The functions to be supported can be paging, sorting, adding, and changing. All functions can be selected.

First, you can write and sort pages by yourself using SQL statements, add and modify the pages, and select all Js statements. It is better to write by yourself, work harder, and learn more! Come on!

In the GridView, how can we achieve both sorting and paging?

It is a simple way to determine the current page directly in the RowDataBound event of the gridview,
The following code is provided:
Protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
If (GridView1.PageIndex = 0) // if the first page is sorted, it starts from 1.
{
If (e. Row. RowIndex! =-1)
{
Int id = e. Row. RowIndex + 1;
E. Row. Cells [0]. Text = id. ToString ();
}
}
Else // otherwise, use the current page to multiply the number of entries per page. For example, if the index on the second page is 1 and the number of entries per page is 5, use the current page to multiply the number of entries per page by 1, the second page starts from 6.
{
If (e. Row. RowIndex! =-1)
{
Int id = (GridView1.PageIndex * number of entries per page) + e. Row. RowIndex + 1;
}
}
}
Hope to help you

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.