Asp.new GridView several ways to get hidden column values

Source: Internet
Author: User

Workaround:

Original from: http://www.tzwhx.com/NewShow/newBodyShow/control _32933.html lerit

1. Get data before hiding columns

Look at an example (see below for example): After the user selects some query criteria, click the "Query" button. The background needs to set the third cell's background color to red, depending on whether the value of the sixth column in each row is 1.

In this way, the background is in the button's click event, go to the database to fetch records, then get a DataTable, and finally bind it to the GridView. If we need to take the value of the hidden column in the RowDataBound event of the GridView, it is no problem to use the Visible property to set a column as hidden. The following code:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24     protected void Btn_Query_Click(object sender, EventArgs e) {DataTable dt = new DataTable();dt=ConstructActiveDataTable();//一个取记录的方法GV.DataSource = dt; GV.DataBind();GV.Columns[5].Visible = false;//设置第6列为隐藏}protected void GV_UpdateData_RowDataBound(object sender, GridViewRowEventArgs e) {    if (e.Row.RowType == DataControlRowType.DataRow)     {       if (e.Row.Cells[5].Text.ToString() == "1")//当行中的第六个单元格的值为1时(第六列为隐藏哦)         {           e.Row.Cells[2].BackColor = System.Drawing.Color.Red;//设置行中第三个单元格背景色为红色         }     } }

Why is this possible? The fact is that you took the value before hiding the column, and then the column was hidden. Because in the Click event, when Gv.databind () is not executed gv.columns[5]. Visible = False, but immediately triggers the RowDataBound event, and then executes the code of the hidden column to go back to execution. Therefore, in this particular problem, you can avoid problems that cannot be evaluated by taking advantage of the sequential relationships of events or function calls.

Of course, this solution is not generic, it requires code execution order.

2. Hide each cell in a column individually

Sometimes we don't want to bind the GridView using the Manual (DataBind ()) above, but instead use some existing data source controls, such as the ObjectDataSource Control. Specifies the DataSourceID of the GridView as the ID of the ObjectDataSource control. The ObjectDataSource control specifies that its SelectMethod property is a function of a return type that is a collection type (for example, a DataTable is returned), and that the function is automatically called and bound in the background (the method does not say).

So it's still the application above, setting the background color of another cell based on the hidden column values, which is not possible, because a binding like DataBind is implicitly executed, although he may also trigger the RowDataBound event immediately after binding, and then go back and execute the rest of the code. However, we cannot set the column-hidden code in the remaining code as in the insert above. Therefore, it is not possible to rely on the control code order, which is the first problem. In addition, there is a problem (referred to in the question ), that is, this control is strange, if you are binding with ObjectDataSource, set the visible of the column once, then ObjectDataSource will bind the data again, So if you hide 10 columns, it is not going to re-read from the database and other data sources repeated 10 times the data, this performance is unacceptable.

For these two reasons, there must be another way to hide the column.

Since the column is formed by the cell, then one by one hidden cells must also be able to achieve hidden effects, but can be hidden after the value of the can be obtained, verified, it is true. Note, however, that cells in the Footer,header and DataRow need to be hidden, or the table will be misplaced. Hidden code can be put anywhere, it is recommended to put in the RowDataBound event, so that each generation of a row to hide the corresponding column.

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21     protected void GV_UpdateData_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header || //如果设置gridview不显示Header,就不写这个(否则报错)    e.Row.RowType == DataControlRowType.DataRow ||    e.Row.RowType == DataControlRowType.Footer)    //如果设置gridview不显示Footer,就不写这个(否则报错)       {    e.Row.Cells[5].Visible=false;//设置每一行中第六个单元格为隐藏,达到隐藏第六列的目的    }   if (e.Row.RowType == DataControlRowType.DataRow)     {       if (e.Row.Cells[5].Text.ToString() == "1")//即使隐藏了,依然可以访问到数值哦         {           e.Row.Cells[2].BackColor = System.Drawing.Color.Red;//设置行中第三个单元格背景色为红色         }     } }
?
1  

There are still people who think this code is not good enough, some people do not like the use of events, but also worry about performance issues, after all, each line to trigger this event.

3. Take advantage of the new attributes DataKeys and DataKeyNames

In fact, Microsoft has made more thoughtful considerations. It provides two new properties for the GridView inability to provide row primary keys: DataKeys and datakeynames! The description in the SDK is as follows:
DataKeyNames: Gets or sets an array that contains the names of the primary key fields of the items that are displayed in the GridView control.
DataKeys: Gets a collection of DataKey objects that represent the data key values for each row in the GridView control.

That is, with DataKeyNames, you can set up a column that is used as the primary key field for the row (where the primary key is not really appropriate, because the values are allowed to be duplicated), and then the values of those columns are accessed by using DataKeys. So, with these two new attributes, we can continue to use the Visible property setting of the column to hide the column while accessing the value of the hidden column. Here's how:

?
1 2 3 4 5 6 7 8     GV.Columns[5].Visible = false;//设置第6列为隐藏 //只需要在设置列为隐藏的地方多加一段代码: GV.DataKeyNames = new string[] { "stateid_O2" }; //假设第六列的列名为stateid_O2protected void GV_UpdateData_RowDataBound(object sender, GridViewRowEventArgs e) {    if (GridView_UpdateData.DataKeys[e.Row.DataItemIndex]["stateid_O2"].ToString() == "1")
?
1 2 3) 4 5     //利用这个DataKeys属性来访问隐藏列的值 {    e.Row.Cells[2].BackColor = System.Drawing.Color.Red;//设置行中第三个单元格背景色为红色  }}

4. Using client code to hide columns

In fact, we are on the server side of the use of a variety of methods to hide the column, then this method is the server side does not set the visibility of the column, then obviously there is no way to value the problem, then let the user do not see some columns, this requires the client's code CSS to achieve the hidden effect. Can be deduced from the above method, can be used to hide the column directly from the CSS, but also through the hidden column of the cell to be implemented. First you need a CSS:

?
1 2 3     <style type="text/css">    .hidden { display:none;}</style>

If the column of the GridView is determined beforehand, that is, it is added through the designer, you only need to footstyle,headerstyle,itemstyle the CssClass property of the corresponding column to "hidden" at design time. As shown in the following:

Of course, if the column is dynamic, or if it hides which column is only determined after binding, then the CSS must be set in the background. There are two ways of setting up:

One is to set the CSS for the column :

?
1 2 3 4 5 6 protected void GridView_UpdateData_RowDataBound(object sender, GridViewRowEventArgs e) {  GV.Columns[5].HeaderStyle.CssClass = "hidden";  GV.Columns[5].ItemStyle.CssClass = "hidden";  GV.Columns[5].FooterStyle.CssClass = "hidden";                        }

The other is to set the CSS for the cell :

?
1 2 3 4 5 6 7 8 9 10 11 12     protected void GV_UpdateData_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header || //如果设置gridview不显示Header,就不写这个(否则报错)   e.Row.RowType == DataControlRowType.DataRow ||    e.Row.RowType == DataControlRowType.Footer)    //如果设置gridview不显示Footer,就不写这个(否则报错)       {    e.Row.Cells[5].CssClass = "hidden";    } }

Summarize:

The first method is less generic and requires the value to be accessed before the column is hidden, which is a constraint condition. The second is to achieve efficiency by hiding each cell in the column from the server side, the third one should be the standard way, use the new attribute to achieve the purpose, need to familiarize himself with his usage; the last is to hide it on the client, but the data is still uploaded to the client, if you don't mind the amount of data that's a little bit more, This should be the easiest to understand and use.

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.