2012-02-19 53 views
5

我想我的GridView列的值更改爲主動當值爲1 我有一個像如何訪問rowdatabound上的gridview列?

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

和CS代碼gridview的列

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

這是工作,但它會如果我更改列順序失敗。 我需要的是類似findControl函數的東西。 謝謝。

回答

15

這裏的一些公用事業我幾年前寫的,可以幫助你:

// ---- 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

的第一個功能'GetCellByName',只應叫時,它的標題行類型:'e.Row.RowType == DataControlRowType.Header' – 2013-09-25 22:43:25

+0

@DavidFreitas不這麼認爲。它使用給定單元格的基礎DataControlField。 – 2013-09-26 04:06:00

3

你可以循環中的所有列,以獲得正確的索引或使用LINQ:

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

清潔更容易閱讀LINQ。蒂姆的沒有爲我工作。

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