Before reading this article, I get the value of a column of a GridView row in the general way: row. CELLS[3]. Text.tostring (). A bit of a fool hehe
added a new data-bound control in asp: GridView, which is intended to replace the DataGrid control in asp.net1.x.
gets the value of a column in the GridView method as
protected void Gridview1_rowediting (object sender, GridViewEditEventArgs e)
{
String id = Gridview1.rows[e.neweditindex]. Cells[0]. Text;
Response.Redirect ("templetedit.aspx?id=" + ID);
}
Similarly, the method to get the key value in the GridView is
protected void Gridview1_selectchanging (object sender, GridViewEditEventArgs e)
{
String id = Gridview1.datakeys[e.newselectindex]. Value.tostring ();
Response.Redirect ("templetedit.aspx?id=" + ID);
}
However, this method cannot get the values of hidden columns and template columns.
However, it is very uncomfortable that if a column is set to Visible=false, then data binding is not done, which means that the text within the column cannot be taken directly from the GridView.
This feature is almost necessary in the DataGrid era, which is useful for batch selection of lists (such as bulk deletion), where hidden columns are typically used to store the values of the key for the data record of a DataGrid row, but now it does not work in the GridView. A huge headache for a large number of people, the Internet also has its solution, one of the following, the principle is that the line is created when its CSS style is not visible, so as to avoid the direct setting of the column Visible=false data unbound problem:
void Gridview1_rowcreated (object sender, GridViewRowEventArgs e)
{
Making the column with index 0 invisible is actually the resulting CSS code
E.row.cells[0]. Visible = false;
Other code if neccecary
}
personally think this method is not elegant, In the 1.x era, I tried to avoid the use of events, preferring to do a loop traversal in the data source DataTable to do some or all of the processing. One thing is that because vs2003 has poor support for web standards, and I'm used to working in HTML source mode, adding an event to the DataGrid is a hassle, and because of its performance I'm not so sure that every line is going to trigger an event. Thus in the GridView still do not like the event, and even if the vs2005 to the Web standard support greatly improved, but add an event easy, delete an event or to be manually deleted in two files in order to synchronize, more troublesome.
In fact, back to the title, now provide my solution to this problem, the basic idea is still to use CSS to make the cell invisible, without interfering with its data binding, but my method does not need to add a line of code in the CS file, the idea is as follows:
1> set up a CSS class:
. hidden {display:none;}
In the Web standard rampage now I want to link for each aspx a COMMON.CSS is the basic quality, hehe
2> then in the GridView Column Edit dialog box, set the columns that need to be hidden, setting the Footstyle,headerstyle,itemstyle CssClass property to "hidden", respectively
Ok, so that we can achieve the purpose of the hidden column, but also to ensure that it is data binding.
Well, see here, if you have something to gain, and be glad to hurry back, add this setting for your GridView, avoid using events to hide columns, and implement the bulk delete feature. Then you're wrong, huh?
Unfortunately, on the Internet to see a lot of people about the GridView function of this discussion, a large part of the people are for this function.
Yes, in the DataGrid, to implement this function, it is necessary to hide the column to store the key value, even a skill that a beginner needs to grope for. However, the GridView is now used instead of the DataGrid, and Microsoft has made more thoughtful considerations. For a problem where the DataGrid cannot provide row primary keys, it provides two entirely new properties: 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.
See here, perhaps you will understand , for the problem of batch check operation, we have a new scenario, now look at a simple implementation, assuming that the primary key field is the ID:
Sets the primary key field name array, which can be multiple fields,
You can also set the primary key field directly in the designer, with multiple fields separated by commas
Gridview1.datakeynames = new string[] {"id"};
Thus, when data binding is done, the GridView automatically populates the DataKeys collection with key values, so that the DataKeys collection can be used to get the key value of a row after binding or callback, for example, assuming that you want to get the ID key value for line I, whose code is as follows:
Gets the ID key value for line I, noting that the index is the index of the row, which counts from the header to the footer
string id=gridviews1.datakeys[i]["id"]. ToString ();
Data is processed by key values ...
Thus, the GridView provides a new mechanism to bind data outside the quota to each row. This makes it possible to completely abandon the original DataGrid by hiding columns to bind key values, obviously, now the scheme is much more elegant.
This article from: it knows the net (http://www.itwis.com) detailed source reference: http://www.itwis.com/html/net/net20/20081105/2730.html