How to access a gridview column on rowdatabound ?

心不动则不痛 提交于 2019-12-09 04:53:26

问题


I would like to change the value of my gridview column to active when the value is 1. I have gridview column like

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

and cs code

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

This is working but it would fail if i change the column order. What i need is something like findControl function. Thanks.


回答1:


Here's a few utilities I wrote years ago that may help you:

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



回答2:


You could loop all columns to get the correct index or use this 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:


Cleaner easier to read LINQ. Tim's did not work for me.

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


来源:https://stackoverflow.com/questions/9349620/how-to-access-a-gridview-column-on-rowdatabound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!