2012-02-19 19 views
5

Tôi muốn thay đổi giá trị của cột GridView của tôi để tích cực khi giá trị là 1. Tôi có cột GridView nhưLàm thế nào để truy cập vào cột GridView trên rowdatabound?

<asp:BoundField DataField="STATUS" HeaderText="STATUS" SortExpression="STATUS" HeaderStyle-HorizontalAlign="Left"> 
       <HeaderStyle HorizontalAlign="Left"></HeaderStyle> 
      </asp:BoundField> 

và cs đang

protected void gvCategory_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 

     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      if (e.Row.Cells[5].Text=="0") 
      { 
       e.Row.Cells[5].Text = "INACTIVE"; 
      } 
     } 
    } 

này đang làm việc nhưng nó sẽ thất bại nếu tôi thay đổi thứ tự cột. Những gì tôi cần là một cái gì đó giống như hàm findControl. Cảm ơn.

Trả lời

15

Dưới đây là một vài tiện ích tôi đã viết năm trước đây có thể giúp bạn:

// ---- GetCellByName ---------------------------------- 
// 
// pass in a GridViewRow and a database column name 
// returns a DataControlFieldCell or null 

static public DataControlFieldCell GetCellByName(GridViewRow Row, String CellName) 
{ 
    foreach (DataControlFieldCell Cell in Row.Cells) 
    { 
     if (Cell.ContainingField.ToString() == CellName) 
      return Cell; 
    } 
    return null; 
} 

// ---- 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 ---------------------------------- 
// 
// 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; 
} 
+0

Đối với chức năng đầu tiên 'GetCellByName', mà chỉ nên được gọi khi đó là Header hàng loại: 'e.Row.RowType == DataControlRowType.Header' –

+0

@DavidFreitas không nghĩ vậy. Nó sử dụng DataControlField cơ bản của một ô đã cho. –

3

Bạn có thể lặp tất cả các cột để có được chỉ số đúng hoặc sử dụng LINQ này:

String colToFind = "status"; 
int colIndex = ((GridView)sender).Columns.Cast<DataControlField>() 
       .Where((c, index) => c.HeaderText.ToLower().Equals(colToFind)) 
       .Select((c,index)=>index).First(); 
0

Cleaner dễ dàng hơn để đọc LINQ. Tim đã không làm việc cho tôi.

private int GetFirstGridViewColIndex(string dataField, object sender) 
{ 
    var boundFieldColumns = ((GridView)sender).Columns.Cast<BoundField>(); 

    return boundFieldColumns.Where((column, index) => string.Equals(column.DataField, dataField, StringComparison.InvariantCultureIgnoreCase)) 
     .Select((column, index) => index) 
     .First(); 
}