. Net code bind the gridview

Source: Internet
Author: User

I tried the traditional data connection method and data display yesterday, but the code looks a bit ASP. Today I will try the data binding of the Asp.net Server Control gridview!
Drag a gridview control and write code in page_load. Of course, I manually bind the code. dataadapter is used here, and its constructor accepts an SQL string and connection object, you do not need to open or close the connection object. dataadapter will process the object and use dateset. Here, an empty dateset object is created and data is filled by calling the fill method of dataadapter, then, you can bind the data by setting the data source of the gridview and calling its databind method.

 
 
  1. OleDbConnection conn = new OleDbConnection();  
  2. conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("db.mdb");  
  3. OleDbDataAdapter da=new OleDbDataAdapter("select art_title,art_author from [article]",conn);  
  4. DataSet ds=new DataSet();  
  5. da.Fill(ds);  
  6. GridView1.DataSource=ds;  
  7. GridView1.DataBind(); 
Generate a website to see the effect!
However, we can see that the table title is the field name. How can we set the table title?
Switch to the design view, select the control, and add a new column. Select boundfield as the field type. The header is the table title, and the data field is the database field to be bound, set the autogeneratecolumns attribute of the gridview to false. This is to avoid the automatic generation of columns in the gridview, and the same data is displayed at the same time. You must set this attribute when customizing the display fields!
Generate a new site, browse, OK, no problem!

If you are not satisfied with this, go further and add pagination. My friends who have written ASP know that they are familiar with Ctrl + C and CTRL + V. If they are new users, they have to write them slowly!
However, the. NET gridview is not that complicated. It only requires simple settings and a code.
In the pagersettings column of the gridview, set the allowpaging attribute of the gridview to true, and then set the pagesize attribute of the number of displayed records to display the position attribute. If mode is set, at the same time, the text or image of the paging link is defined in sequence. In this way, the settings are complete. The last step is to add the pageindexchanging event of the gridview. That is, when you click the paging Link, what the gridview has to do is to read the code:
 

 
 
  1. protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  2. {  
  3.     GridView1.PageIndex = e.NewPageIndex;  
  4. }  
Regenerate the website. You can see that the table has been added to the page!

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.