Use of the DataGrid control--Paging + data binding (i)

Source: Internet
Author: User
Tags bind advantage

These days to learn how to bind the data, and then the bound data for paging processing, has been difficult to understand before, but hands-on practice only to find that it is not difficult. The following is a brief introduction to the use of the DataGrid control. First, select the DataGrid control in the menu bar

If not added, right-click the tab, and then add the DataGrid control to the Toolbox:

As shown in the following illustration: Note to select. NET Framework Components


Ii. implementing data binding and pagination processing

As shown in the following illustration:




The advantage of the DataGrid control is that it automatically encapsulates a feature about paging, without requiring us to write code, click Settings directly



Here's a description of our code: For convenience, we bind data to a grid and encapsulate it as a method, the name of which is our control's name:

<span style= "FONT-SIZE:18PX;" >private void BindToDataGrid1 ()
        {
            //binds data to the grid
            SqlConnection con = Dbc.createcon ();
            SqlDataAdapter SDA = new SqlDataAdapter ();
            Sda. SelectCommand = new SqlCommand ("SELECT * from News", con);
            DataSet ds = new DataSet ();
            Sda. Fill (ds, "emp");
            This. DataGrid1.DataSource = ds. tables["EMP"];
            This. Datagrid1.databind ();
        } </span>
For when we click on the next page, there will be different data, we use a pageindexchanged

<span style= "FONT-SIZE:18PX;" >        protected void datagrid1_pageindexchanged (object source, DataGridPageChangedEventArgs e)
        {
           // The current number is how much this 
            . Datagrid1.currentpageindex = E.newpageindex;
            This. BindToDataGrid1 ();
        } </span>
Every time we click on the next page, we reload the page.

<span style= "FONT-SIZE:18PX;" >        protected void Page_Load (object sender, EventArgs e)
        {
            if (! Page. IsPostBack)
            {this
                . BindToDataGrid1 ();
            }


        }
</span>
Okay, it's almost over. Using the DataGrid control for data binding and paging, let's look at the effect:

Summary:

The advantage of paging in the DataGrid control is that it is easy to operate and can be visualized, which makes it easy for us to use, but it is also what we call false paging, and it is easy to run slow when processing large amounts of data.

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.