search
HomeBackend DevelopmentC#.Net TutorialDetailed introduction to 'ASP.NET' data binding - GridView

GirdView Introduction:

Name: Network View.

Source: GridView is the successor control of DataGrid. In .net framework 2, although DataGrid still exists, GridView has come to the forefront of history, and the trend of replacing DataGrid is inevitable. Don't block.

Function: Its function is to display the data in the data source in the web page. GridView and DataGrid have similar functions. They both display data from the data source on the web page, and display a row of data in the data source, that is, a record, as a row in the output table on the web page.

I will not elaborate on the detailed properties and events of GirdView here. Below I will just briefly introduce how GirdView displays the data searched from the background database, that is, how GirdView binds and displays the data source.

1. The front-end interface is as follows


## 2. Back-end Writing: Use VS to build ASP.NET form applications. 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 an operation class, which includes ordinary query methods, query methods by conditions, and add 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. Write the query button Click event

        protected void Button4_Click(object sender, EventArgs e)
        {
            string c = "";    //定义空字符串,用来条件查询
            
             //设置复选框1的查询条件
            if (this.CheckBox1.Checked)
            {
                c = "pID=" + this.txtID.Text;   //精确匹配查询条件
            }
            else
            {
                c = "pID like'%' ";        //模糊匹配查询条件
            }
            if (this.CheckBox2.Checked)
            {
                c += " and personName like '%" + this.txtName.Text + "%'";
            }
            if (this.CheckBox3.Checked)
            {
                if (RadioButton1.Checked)
                {
                    c += "and personSex='男'";
                }

                else
                {
                    c += "and personSex='女'";
                }
            }

            DataView dv = new DataView(PerosonOperate.SelectAll()); //调用查询方法
            dv.RowFilter = c;                                       //设置过滤器(按条件查找)
            dv.Sort = "pID Desc";                                   //使结果按照pID字段降序排列
            GridView1.DataSource = dv;                              //设定数据源
            GridView1.DataBind();                                   //绑定数据源
            //设置列名,如果不设置,将会以数据库中对应的字段名称代替
            GridView1.HeaderRow.Cells[0].Text = "编号";
            GridView1.HeaderRow.Cells[1].Text = "姓名";
            GridView1.HeaderRow.Cells[2].Text = "性别";


The three query renderings are as follows, namely direct click query, query by gender, query by number and Query name and gender together.



## The background code written above It is only written on the basis that the functions can be realized. There are some bugs in it. I hope everyone can modify it by themselves.

From the filtering of background bound data to the presentation of data in the foreground, the general process of using GridView to display data on the browser is like this. The only thing that is a bit awkward here is the conditional In the query, the spelling of the string is difficult. This is nothing more than using the filtering effect of GirdView, which is this code

dv.RowFilter = c; I hope everyone will be more careful in code writing.


##

The above is the detailed content of Detailed introduction to 'ASP.NET' data binding - GridView. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
C# and .NET: Understanding the Relationship Between the TwoC# and .NET: Understanding the Relationship Between the TwoApr 17, 2025 am 12:07 AM

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

The Continued Relevance of C# .NET: A Look at Current UsageThe Continued Relevance of C# .NET: A Look at Current UsageApr 16, 2025 am 12:07 AM

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

From Web to Desktop: The Versatility of C# .NETFrom Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# .NET and the Future: Adapting to New TechnologiesC# .NET and the Future: Adapting to New TechnologiesApr 14, 2025 am 12:06 AM

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

Is C# .NET Right for You? Evaluating its ApplicabilityIs C# .NET Right for You? Evaluating its ApplicabilityApr 13, 2025 am 12:03 AM

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

C# Code within .NET: Exploring the Programming ProcessC# Code within .NET: Exploring the Programming ProcessApr 12, 2025 am 12:02 AM

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

C# .NET: Exploring Core Concepts and Programming FundamentalsC# .NET: Exploring Core Concepts and Programming FundamentalsApr 10, 2025 am 09:32 AM

C# is a modern, object-oriented programming language developed by Microsoft and as part of the .NET framework. 1.C# supports object-oriented programming (OOP), including encapsulation, inheritance and polymorphism. 2. Asynchronous programming in C# is implemented through async and await keywords to improve application responsiveness. 3. Use LINQ to process data collections concisely. 4. Common errors include null reference exceptions and index out-of-range exceptions. Debugging skills include using a debugger and exception handling. 5. Performance optimization includes using StringBuilder and avoiding unnecessary packing and unboxing.

Testing C# .NET Applications: Unit, Integration, and End-to-End TestingTesting C# .NET Applications: Unit, Integration, and End-to-End TestingApr 09, 2025 am 12:04 AM

Testing strategies for C#.NET applications include unit testing, integration testing, and end-to-end testing. 1. Unit testing ensures that the minimum unit of the code works independently, using the MSTest, NUnit or xUnit framework. 2. Integrated tests verify the functions of multiple units combined, commonly used simulated data and external services. 3. End-to-end testing simulates the user's complete operation process, and Selenium is usually used for automated testing.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft