[ASP. NET] How to Use columnname instead of row. cells [columnindex] of gridview
If you have used the DataGrid, you must remember that you can directly retrieve cell (string columnname) from the cursor.
This thing disappears in the gridview layout. E. Row. cells [int columnindex] can only be used to obtain the location of the second digit.
In scenarios where the program needs to change to a complex one, or when the memory storage space and the grid header need to be merged, the index is regarded as very troublesome, and usually has one more column, when index indexes are reached, they still miss.
Therefore, it is still necessary to design this demand in the case of big data.
Unfortunately, it does not seem to be possible to directly expand the cells () method. (I really want to design more features ~~~~)
When talking about the Internet and Ms, it is almost always about using function compute for external use. Of course, the efficiency must be poor, but the columns of a grid should not be enough normally,
Efficiency is a little more efficient than the cost of optimization.
In this example, two versions are provided, provided that columnname cannot have a duplicate, and function is directly configured in the gridview.
If you do not want to upload data in the gridview, change this to the ID of your gridview.
- Evaluate the datafield atomicity of boundfield, which is only usable by boundfield.
View Source
Print?
02 |
/// Gets the index of the datafield column name matched. |
04 |
/// <Param name = "datacolumnname"> name of the data column. </param> |
05 |
/// <Returns> </returns> |
06 |
Public Int Getdatafieldcolumnindex ( String Datacolumnname) |
08 |
Datacontrolfieldcollection columns = This . Columns; |
10 |
Foreach (Datacontrolfield Field In Columns) |
12 |
If (Field Is System. Web. UI. webcontrols. boundfield) |
14 |
If (Boundfield) Field). datafield = datacolumnname) |
16 |
Columnindex = columns. indexof (field ); |
- Returns the headertext adequacy of the column. Even if it is not boundfield, the cursor can still be obtained. It is possible that there may be Chinese characters or headertext. If it is an animated word or a multi-country speaker, it is a bit difficult. View Source
Print?
02 |
/// Gets the index of the headertext column name matched. |
04 |
/// <Param name = "columnheadertext"> the column headertext. </param> |
05 |
/// <Returns> </returns> |
06 |
Public Int Getheadertextcolumnindex ( String Columnheadertext) |
08 |
Foreach (Datacontrolfield Column In This . Columns) |
10 |
If (Column. headertext = columnheadertext) |
12 |
Int Columnid = This . Columns. indexof (column ); |
Usage example: value is my second largest datafield.
View Source
Print?
1 |
Protected Void Button#click ( Object Sender, eventargs E) |
3 |
This . Button1.text = This . Joeygridview1.rows [1]. cells [ This . Joeygridview1.getdatafieldcolumnindex ( "Value" )]. Text; |