The GridView control bound to ASP. NET data, asp. netgridview

Source: Internet
Author: User

The GridView control bound to ASP. NET data, asp. netgridview

The GridView is the successor control of the DataGrid. In. net framework 2, although the DataGrid still exists, the GridView has been on the foreground of history, and the trend of replacing the DataGrid is not blocked.
Purpose:The function is to display data in the data source on the web page. Similar to the DataGrid function, the GridView displays data in the data source on the web page. A row of data in the data source, that is, a record, is displayed as a row in the output table on the web page.
I will not elaborate on the detailed attributes and events of GirdView here. Next, I will briefly introduce how GirdView displays the data searched from the background database, that is, how GirdView binds and displays the data source.
I. the foreground interface is as follows:

2. Background writing: Use VS to create an ASP. NET form application. Here, I only write the query function. The background code is as follows:
1. Establish a database connection

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

2. Write operations, including common query methods, conditional query methods, and addition methods (omitted)

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. Compile the query button and click the event

Protected void Button4_Click (object sender, EventArgs e) {string c = ""; // defines an empty string for conditional query // sets the query condition if (this. checkBox1.Checked) {c = "pID =" + this.txt ID. text; // exact match query condition} else {c = "pID like '%'"; // fuzzy match query condition} if (this. checkBox2.Checked) {c + = "and personName like '%" + this.txt Name. text + "% '";} if (this. checkBox3.Checked) {if (RadioButton1.Checked) {c ++ = "and personSex = 'male'" ;}else {c ++ = "and personSex = 'femal '";}} dataView dv = new DataView (PerosonOperate. selectAll (); // call the query method dv. rowFilter = c; // set the filter (search by condition) dv. sort = "pID Desc"; // Sort the results in descending order by pID field GridView1.DataSource = dv; // set the data source GridView1.DataBind (); // bind the data source // set the column name, if this parameter is not set, GridView1.HeaderRow is replaced by the field name in the database. cells [0]. text = "no."; GridView1.HeaderRow. cells [1]. text = "name"; GridView1.HeaderRow. cells [2]. text = "gender ";

The three queries are as follows: Click query, query by gender, query by number, name, and gender.

The background Code Compiled above is only written on the basis of functions that can be implemented. There are no bugs in it, and we hope you can modify it yourself.
From the screening of bound data in the background to the presentation at the front end, the general process of displaying data in the browser using the GridView is like this. The only thing here is that the query by condition is in progress, the character string cannot be spelled properly. This is simply to use the GirdView filter effect, that is, this code dv. rowFilter = c; I hope you will be careful in coding.

The above example shows you how to use the GridView control in ASP. NET data binding. I hope it will be helpful for your learning.

Articles you may be interested in:
  • Implementation of batch Delete gridview under asp.net
  • Asp.net gridview 72 stunt
  • In the asp.net GridView control, the template column CheckBox is fully selected, deselected, and canceled.
  • Use of superlinks in asp.net GridView (with parameters)
  • ASP. NET Gridview and checkbox all selected, all deselected implementation code
  • Summary of methods for obtaining row indexes in the Rowcommand of asp.net gridview
  • Asp.net simple code sets the implementation idea and code of the GridView adaptive column width without deformation
  • Text Content in ASP. NET GridView cannot wrap (automatic line feed/normal line feed)
  • Instance code for querying, paging, editing, updating, and deleting the gridview in asp.net
  • Asp.net GridView usage (pagination)

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.