[ASP. NET] How to Use columnname instead of row. cells [columnindex] of gridview

Source: Internet
Author: User
[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.

  1. Evaluate the datafield atomicity of boundfield, which is only usable by boundfield.

    View Source

    Print?

    01 /// <Summary>
    02 /// Gets the index of the datafield column name matched.
    03 /// </Summary>
    04 /// <Param name = "datacolumnname"> name of the data column. </param>
    05 /// <Returns> </returns>
    06 Public Int Getdatafieldcolumnindex (String Datacolumnname)
    07 {
    08 Datacontrolfieldcollection columns =This. Columns;
    09 Int Columnindex =-1;
    10 Foreach (Datacontrolfield FieldIn Columns)
    11 {
    12 If (FieldIs System. Web. UI. webcontrols. boundfield)
    13 {
    14 If (Boundfield) Field). datafield = datacolumnname)
    15 {
    16 Columnindex = columns. indexof (field );
    17 }
    18 }
    19 }
    20 Return Columnindex;
    21 }
  2. 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?

    01 /// <Summary>
    02 /// Gets the index of the headertext column name matched.
    03 /// </Summary>
    04 /// <Param name = "columnheadertext"> the column headertext. </param>
    05 /// <Returns> </returns>
    06 Public Int Getheadertextcolumnindex (String Columnheadertext)
    07 {
    08 Foreach (Datacontrolfield ColumnIn This. Columns)
    09 {
    10 If (Column. headertext = columnheadertext)
    11 {
    12 Int Columnid =This. Columns. indexof (column );
    13 Return Columnid;
    14 }
    15 }
    16 Return -1;
    17 }
      Usage example: value is my second largest datafield.
    View Source

    Print?

    1 Protected Void Button#click (Object Sender, eventargs E)
    2 {
    3 This. Button1.text =This. Joeygridview1.rows [1]. cells [This. Joeygridview1.getdatafieldcolumnindex ("Value")]. Text;
    4 }
Related Article

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.