[Import] an elegant solution for hiding column values in the GridView

Source: Internet
Author: User

Added a new data binding control in Asp.net 2.0: GridView, which is used to replace Asp. the DataGrid Control in net1.x, but it is quite uncomfortable that if a column is set to visible = false, no data binding will be performed, that is to say, the text in this column cannot be obtained directly from the GridView.
This feature is almost essential in the DataGrid era and is useful for batch selection of lists (such as batch deletion ), hidden columns are usually used to store the values of the key words corresponding to the data record of the row in the DataGrid, but they do not work in the GridView, which is a headache for a large number of people. They also have their solutions online, one of them is as follows. The principle is to set its css style to invisible when creating a row, so as to avoid the data not binding problem caused by directly setting column visible = false:

Void GridView1_RowCreated (object sender, GridViewRowEventArgs e)
{
// Make the column with the index 0 invisible, which is actually the generated css code
E. Row. Cells [0]. Visible = false;
// Other code if neccecary
}

I personally think this method is not elegant enough. In Asp.net, I tried to avoid using events as much as possible. I would rather do loop traversal in the data source able for some annoying or simple processing. First, because vs2003 has poor support for web standards, and I am used to working in the Html source code mode, it is extremely troublesome to add an event to the DataGrid. Second, I am not very worried about its performance, after all, each row triggers an event. Therefore, you still do not like events in the GridView. Even if vs2005 greatly improves the web standard support, it is easy to add an event, to delete an event, you must manually delete it from two files to synchronize the event. This is troublesome.

On the Subject Matter, return to the title and provide a solution to this problem. The basic idea is to use css to make cells invisible without interfering with data binding, however, my method does not need to add a line of code to the cs file. The idea is as follows:
(1) set a css class:

. Hidden {display: none ;}

Now that the Web standard is horizontal, I think connecting each aspxchain to common.css is a basic quality.
(2) then, in the column editing dialog box of the GridView, set the columns to be hidden and set the CssClass attribute of FootStyle, HeaderStyle, and ItemStyle to "hidden"
OK, so that we can implement the purpose of hiding columns and ensure data binding.

Now, if you have some gains, and are happy to rush back immediately, add this setting for your GridView to avoid using events to hide columns, the batch deletion function is also implemented. That's a big mistake.
It is a pity that many people have discussed the function of the GridView on the Internet. A large part of people come for this function.

Yes. To implement this function in the DataGrid, it is necessary to hide columns to store key values. It is even a skill that beginners need to explore. However, the current GridView is used to replace the DataGrid, and Microsoft has made more comprehensive considerations. Because the DataGrid cannot provide the row primary key, it provides two completely new attributes: DataKeys and DataKeyNames! The SDK description is as follows:
DataKeyNames: gets or sets an array that contains the name of the primary key field of the item displayed in the GridView control.
DataKeys: gets a set of DataKey objects that represent the data key values of each row in the GridView control.

Here, you may understand that we have a new solution for the batch selection operation. Now let's look at a simple implementation, assuming that the primary key field is id:

// Set an array of primary key field names. Multiple fields are allowed,
// You can also set primary key fields in the designer. Separate multiple fields with commas (,).
GridView1.DataKeyNames = new string [] {"id "};

In this way, when data is bound, the GridView will automatically fill the DataKeys set with the key value, so that the key value of a row can be obtained through the DataKeys set after binding or returning, for example, if you want to obtain the id key value of row I, the Code is as follows:

// Obtain the id key value of row I. Note that this index is the index of the row, including header and footer.
String id = GridViews1.DataKeys [I] ["id"]. ToString ();
// Process data using key values ......

It can be seen that the GridView provides a new mechanism to bind additional data to each row. This allows you to completely discard the practice of binding a key value to a DataGrid by hiding columns. Obviously, the current solution is much more elegant. Of course, if you really need to use hidden columns in the GridView and can take values, I will not try the above method, although it is difficult for me to think of such a requirement ^_^

Comments by programmers

Source: http://www.cnblogs.com/greateast/archive/2007/04/11/709273.html

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.