Asp. NET Data Binding GridView Control _ Practical Tips

Source: Internet
Author: User

The GridView is the successor control of the DataGrid, and in the. NET Framework 2, although there is a DataGrid, the GridView is already at the forefront of history, and the trend to replace the DataGrid has become irresistible.
role: The function is to display data from a data source in a Web page. The GridView and DataGrid functions are similar in that they display data from a data source in a Web page, and a row of data from a data source, that is, a record, is displayed as a row in the output table on a Web page.
In this girdview the detailed properties and events I no longer elaborate. Here's a brief introduction to Girdview how to display data from a background database, that is, how girdview binds and displays the data source.
first, the front interface is as follows

Second, background writing: using VS to establish asp.net form applications. Here, I only write the query function, the background code is as follows
1. Establish database connection

public static SqlConnection createconnection () 
 { 
   SqlConnection con = new SqlConnection ("server=.; database=dropdowntest;uid=sa;pwd=123456 "); 
   Con. Open (); 
   return con; 
 } 

2, the writing Operation class, which has the ordinary query method, according to the condition inquiry method, the addition method (slightly)

public static DataTable SelectAll () 
{ 
  SqlConnection con = createconnection (); 
  DataTable dt = new DataTable (); 
  SqlCommand cmd = new SqlCommand ("SELECT * from person", con); 
  SqlDataReader SDR = cmd. ExecuteReader (); 
  Dt. Load (SDR); 
  return DT; 

3. Write Query button click event

protected void Button4_Click (object sender, EventArgs e) {string c = ""; Defines an empty string that is used to condition query//Set check box 1 for query conditions if (this.  checkbox1.checked) {c = "pid=" + this.txtID.Text;    Exact match query condition} else {c = ' PID like '% '; Fuzzy matching query conditions} if (this. 
  checkbox2.checked) {c + = "and personname like '%" + this.txtName.Text + "%"; } if (this. 
    checkbox3.checked) {if (radiobutton1.checked) {c = "and personsex= ' man"; 
    else {c = "and personsex= ' female"; } DataView dv = new DataView (Perosonoperate.selectall ()); Call Query method DV.                    RowFilter = C; Sets the filter (find by Condition) DV.                  Sort = "PID Desc";               Aligns the results in descending order with the PID field gridview1.datasource = DV;                  Set the data source Gridview1.databind (); Bound data source//Set column name, if not set, will be the corresponding field name in the database instead of gridview1.headerrow.cells[0]. 
  Text = "number"; GRIDVIEW1.HEADERROW.CELLS[1]. 
  Text = "name"; GRIDVIEW1.HEADERROW.CELLS[2]. Text = "gender"; 
 

Three query effects are shown below, respectively, for direct-click inquiries, according to gender inquiries, according to the number and name and sex piece of inquiry.

The background code written above is only in the function can be implemented on the basis of writing, there are some bugs, I hope everyone to transform themselves.
From background binding data filtering to foreground rendering, the general process of using the GridView to display data in a browser is this, and the only thing that's a bit awkward is that the spelling of the string is bad, so it's just using the Girdview filter effect, which is the code DV. RowFilter = C; I hope you'll be more careful in your code writing.

The above is through an example for you to introduce the asp.net data binding in the GridView control method of use, I hope to help you learn.

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.