C# 如何在asp:表中隐藏列?
我有一个简单的ASP.NET表,如下所示:C# 如何在asp:表中隐藏列?,c#,.net,asp.net,C#,.net,Asp.net,我有一个简单的ASP.NET表,如下所示: <asp:Table id="tbl"> <asp:TableHeaderRow id="header"> <asp:TableHeaderCell id="hcell1" /> </asp:TableHeaderRow> <asp:TableRow id="row"> <asp:TableCell id="cell1" />
<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>
不太理想的是执行类似操作的for/foreach循环。标记:
<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>
将runat=“server”放在所有标记上,然后在代码隐藏中执行[control id].Visible=false 尝试使用此扩展方法,它扩展了Table类,添加了按索引和TableHeaderCell ID隐藏列的方法(如果存在): 但是,请注意,它没有提供任何逻辑来满足跨越其他区域的列 栏目: 示例
tbl.HideColumn("HeaderID");
tbl.HideColumn(0);
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);
}
}
课程
tbl.HideColumn("HeaderID");
tbl.HideColumn(0);
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);
}
}
如果您计划使用内置的Delete/Edit/Select命令,并且希望隐藏id列,那么最好在风格上隐藏它 这是我使用的函数
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");
}
}
编辑
和这家伙在一起
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;
}
静态公共int-GetColumnIndex(GridView gv,字符串columnName)
{
int returnMe=-1;
对于(int i=0;i 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);
}
}
此代码也适用于表格不同行中指定的可变列跨度。设置
cell1.Visible=false代码>将仅隐藏该单元格。我想隐藏该列。此外,我更希望我可以使用列的名称,而不必依赖于幻数。