GridView Utilities in C#

 Rate It (1)

GridView Utilities in C#

GetColumnIndexByHeaderText

    // ---- GetColumnIndexByHeaderText ----------------------------------
    //
    // pass in a GridView and a Column's Header Text
    // returns index of the column if found
    // returns -1 if not found

    static public int GetColumnIndexByHeaderText(GridView aGridView, String ColumnText)
    {
        TableCell Cell;
        for (int Index = 0; Index < aGridView.HeaderRow.Cells.Count; Index++)
        {
            Cell = aGridView.HeaderRow.Cells[Index];
            if (Cell.Text.ToString() == ColumnText)
                return Index;
        }
        return -1;
    }

GetColumnIndexByDBName

    // ---- GetColumnIndexByDBName ----------------------------------
    //
    // pass in a GridView and a database field name
    // returns index of the bound column if found
    // returns -1 if not found

    static public int GetColumnIndexByDBName(GridView aGridView, String ColumnText)
    {
        System.Web.UI.WebControls.BoundField DataColumn;

        for (int Index = 0; Index < aGridView.Columns.Count; Index++)
        {
            DataColumn = aGridView.Columns[Index] as System.Web.UI.WebControls.BoundField;

            if (DataColumn != null)
            {
                if (DataColumn.DataField == ColumnText)
                    return Index;
            }
        }
        return -1;
    }

Revision number 1, Saturday, February 23, 2008 7:19:50 PM by
This is not the most up to date version of this article. The most recent version can be found here.

Comments

good

its helpful

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. mbanavige (5)
  2. SGWellens (4)
  3. maartenba (2)
  4. rami_nassar (2)
  5. stiansol (2)
  6. MisterFantastic (2)
  7. satish1.v (1)
  8. raklos (1)
  9. mosessaur (1)
  10. Jos Branders (1)

Advertise Here

Microsoft Communities
Page view counter