65.9K
CodeProject 正在变化。 阅读更多。
Home

C# 和 VB.NET 中的 GridView 工具

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2013 年 10 月 11 日

CPOL
viewsIcon

7921

GridView 工具下面的 `GetColumnIndexByHeaderText` 方法通过传递一个 GridView 控件来查找 GridView 内部的列索引。

GridView 工具

下面的 `GetColumnIndexByHeaderText` 方法通过传递 GridView 实例以及标题文本来查找 GridView 控件内部的列索引。如果未找到列,则返回值为 -1。

C#

    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;
    }

VB.NET

 Public Shared Function GetColumnIndexByHeaderText(ByVal aGridView As GridView, ByVal ColumnText As String) As Integer
  Dim Cell As TableCell
  For Index As Integer = 0 To aGridView.HeaderRow.Cells.Count - 1
   Cell = aGridView.HeaderRow.Cells(Index)
   If Cell.Text.ToString() = ColumnText Then
   Return Index
   End If
  Next Index
  Return -1
 End Function

 

`GetColumnIndexByDBName` 方法通过指定数据库中数据绑定的列名来返回 GridView 控件内部的列索引。返回值将是找到的列索引,否则为 -1。

C#

    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;
    }

VB.NET

 Public Shared Function GetColumnIndexByDBName(ByVal aGridView As GridView, ByVal ColumnText As String) As Integer
  Dim DataColumn As System.Web.UI.WebControls.BoundField
  For Index As Integer = 0 To aGridView.Columns.Count - 1
   DataColumn = TryCast(aGridView.Columns(Index), System.Web.UI.WebControls.BoundField)
   If Not DataColumn Is Nothing Then
    If DataColumn.DataField = ColumnText Then
     Return Index
    End If
   End If
  Next Index
  Return -1
 End Function
© . All rights reserved.