【问题标题】:How to access a gridview column on rowdatabound ?如何访问 rowdatabound 上的 gridview 列?
【发布时间】:2012-02-19 13:34:43
【问题描述】:

我想在值为 1 时将我的 gridview 列的值更改为活动状态。 我有像

这样的 gridview 列
<asp:BoundField   DataField="STATUS" HeaderText="STATUS" SortExpression="STATUS" HeaderStyle-HorizontalAlign="Left">
                <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
            </asp:BoundField>

和cs代码

 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 函数。 谢谢。

【问题讨论】:

    标签: asp.net gridview rowdatabound


    【解决方案1】:

    以下是我多年前编写的一些实用程序,可能会对您有所帮助:

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

    【讨论】:

    • 对于第一个函数GetCellByName,只有当它是标题行类型时才应该调用:e.Row.RowType == DataControlRowType.Header
    • @DavidFreitas 不这么认为。它使用给定单元格的底层 DataControlField。
    【解决方案2】:

    您可以循环所有列以获取正确的索引或使用此 LINQ:

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

    【讨论】:

      【解决方案3】:

      更干净更易阅读的 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();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多