Dynamically change the column name of the GridView Control

Source: Internet
Author: User

In the recent period, many projects have to be written, and more personalized requirements of customers are required. For example, the field name obtained from the database needs to be changed to another name when it is displayed in the GridView. The following example uses similar data instead of the project content, but Insus. NET for demonstration.

First, create a webpage on the webpage of the site and pull the GridView control into the webpage. The field name originally displayed is blue-underlined.



In. cs:



OK, the program running result is as follows:




Now, we need to dynamically change the names of the columns in the simhei font, which are ID, Kind Name, and Fruit Name. What should I change to? This depends on the real requirements of the customer.
It has three columns in total. Therefore, we need to define the Field names of the three columns, which correspond one to one and put them in an array.

[] FieldNames = {,,};


The project environment of Insus. NET is obtained directly from the database. Because of the customization, the customer can easily change the corresponding name.

Next, there is the problem. How can we change it? This is the real problem. Go to the. aspx html page and add an event OnRowCreated = "GridViewFruit_RowCreated" in the GridView control ":




Go to. cs to write the Event Logic Program:



#1 is to determine whether it is the Header of the GridView.
#2 is the row where the Header is located.
#3 Add the Custom column name to the Header.


Run preview to see what it looks like:



The Custom column name appears. But the original column name still exists. How can this problem be solved? Think of it? The Grdiveiw control has a ShowHeader attribute that controls whether to display the Header of the GridView.




OK. Final effect:



Insus. NET hopes that this blog post will help you with your experience.

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.