开发者

How do I hide a column in an asp:Table?

I have a simple ASP.NET table like so:

<asp:Table id="tbl">
    <asp:TableHeaderRow id="header">
        <asp:TableHeaderCell id="hcell1" />
    </asp:TableHeaderRow>
    <asp:TableRow id="row">
        <asp:TableCell id="cell1" />
    </asp:TableRow>
</asp:Table>

The ID's are made up and the actual table has several more columns. I want to be able to hide any column programmatically from the codebehind (not javascript). Is this possible? At this point I can easily change the markup to be pretty much w开发者_StackOverflow社区hatever I want, so I'm open to suggestions.

EDIT: Sorry to be clear. I want to be able to simply hide a column in such a way that if I add a new row I don't want to have to change any of the code that handles the hiding. The ideal would be something like:

tbl.Columns["ColName"].Visible = false;

Less ideal would be a for/foreach loop that did something similar.


Try using this extension method, it extends the Table class, adding methods to hide columns by index and by the ID of a TableHeaderCell (if one exists):

Note however, that it does not provide any logic to cater for columns which span other columns:

Examples

tbl.HideColumn("HeaderID");
tbl.HideColumn(0);

Class

public static class TableExtensions
{
    public static void HideColumn(this Table table, int index)
    {
        foreach (TableRow row in table.Rows)
        {
            if (row.Cells.Count-1 >= index)
            {
                row.Cells[index].Visible = false;
            }
        }
    }

    public static void HideColumn(this Table table, string id)
    {
        int index = 0;
        bool columnFound = false;

        if (table.Rows.Count > 1)
        {
            TableHeaderRow headerRow = table.Rows[0] as TableHeaderRow;
            if (headerRow != null)
            {
                foreach (TableHeaderCell cell in headerRow.Cells)
                {
                    if (cell.ID.ToLower() == id.ToLower())
                    {
                        columnFound = true;
                        break;
                    }

                    index++;
                }
            }
        }

        if(columnFound)
            HideColumn(table, index);
    }
}


put runat="server" on all the tags, and then in code behind you can do [control id].Visible = false;


Markup:

<asp:Table id="tbl" runat="server"> <---!
    <asp:TableHeaderRow id="header">
        <asp:TableHeaderCell id="hcell1" />
    </asp:TableHeaderRow>
    <asp:TableRow id="row">
        <asp:TableCell id="cell1" />
    </asp:TableRow>
</asp:Table>

Code-behind:

foreach(TableRow row in tb1.Rows)
{
    if (row.Columns.Count >= x + 1)
        row.Columns[x].Visible = false;
}


If you are planning on using the built in Delete/Edit/Select commands and you want to hide an id column you would be better off stylistically hiding it.

Here is the function I use

static public void HideColumn(GridView gv, int columnIndex)
{
    if (gv.HeaderRow != null)
        gv.HeaderRow.Cells[columnIndex].Style.Add("display", "none");
    foreach (GridViewRow row in gv.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
            row.Cells[columnIndex].Style.Add("display", "none");
    }
}

Edit Alongside this guy

static public int GetColumnIndex(GridView gv, string columnName)
{
    int returnMe = -1;
    for (int i = 0; i < gv.Columns.Count; i++)
    {
        if (gv.Columns[i].HeaderText == columnName)
        {
            returnMe = i;
            break;
        }
    }
    return returnMe;
}


Adding to the response from @jdavies, the code below also works in case we have column span specified for any column. Also, the code is enhanced to work to either show or hide columns as required.

 public static class TableExtensions
{
    public static void ShowOrHideColumn(this Table table, int index, bool bShowColumn)
    {
        foreach (TableRow row in table.Rows)
        {
            var colIndex = 0;
            var actionCol = 0;
            foreach (TableCell cell in row.Cells)
            {
                if (colIndex == index)
                {
                    row.Cells[actionCol].Visible = bShowColumn;
                    break;
                }
                colIndex += cell.ColumnSpan == 0 ? 1 : cell.ColumnSpan;
                actionCol++;
            }
        }
    }

    public static void ShowOrHideColumn(this Table table, string id, bool bShowColumn)
    {
        int index = 0;
        bool columnFound = false;

        if (table.Rows.Count > 1)
        {
            TableHeaderRow headerRow = table.Rows[0] as TableHeaderRow;
            if (headerRow != null)
            {
                foreach (TableHeaderCell cell in headerRow.Cells)
                {
                    if (cell.ID != null && cell.ID.ToLower() == id.ToLower())
                    {
                        cell.Visible = bShowColumn;
                        columnFound = true;
                        break;
                    }

                    index += cell.ColumnSpan == 0 ? 1 : cell.ColumnSpan;
                }
            }
        }

        if (columnFound)
            table.ShowOrHideColumn(index, bShowColumn);
    }
}

This code will also work for variable column span specified in different rows of the table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜