【发布时间】:2010-10-27 10:17:24
【问题描述】:
我有一个 GridView,它需要显示 GridView 中的 x 列,或者只显示一列(单元格),它将扩展以占据整行。通过检查一列是否为空白来决定显示哪一列 - 如果为空白,则显示其他单元格,如果不是,则仅显示该单元格。
我该怎么做呢?我正在使用 SqlDataSource 来选择 GridView 的内容,但我愿意将其更改为更具编程性的方法。
谢谢
【问题讨论】:
我有一个 GridView,它需要显示 GridView 中的 x 列,或者只显示一列(单元格),它将扩展以占据整行。通过检查一列是否为空白来决定显示哪一列 - 如果为空白,则显示其他单元格,如果不是,则仅显示该单元格。
我该怎么做呢?我正在使用 SqlDataSource 来选择 GridView 的内容,但我愿意将其更改为更具编程性的方法。
谢谢
【问题讨论】:
我不确定我是否理解正确,但如果你愿意,你可以自己建立你的Table。然后,您可以准确控制要包含哪些列(以及哪些数据)。
Table table = new Table();
table.GridLines = GridLines.None;
table.CellPadding = 3;
table.CellSpacing = 0;
// add a header
TableHeaderRow header = new TableHeaderRow();
foreach (string header in new string[] { "column1", "column2" }) {
TableCell cell = new TableCell();
cell.Text = header;
header.Cells.Add(cell);
}
// add data
foreach (var rowd in data) {
TableRow row = new TableRow();
foreach (string columnData in new string[] { rowd.Column1, rowd.Column2 }){
TableCell cell = new TableCell();
cell.Text = columnData;
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
【讨论】: