【问题标题】:Dynamically changing GridView row (ASP.NET,C#)动态更改 GridView 行 (ASP.NET,C#)
【发布时间】:2010-10-27 10:17:24
【问题描述】:

我有一个 GridView,它需要显示 GridView 中的 x 列,或者只显示一列(单元格),它将扩展以占据整行。通过检查一列是否为空白来决定显示哪一列 - 如果为空白,则显示其他单元格,如果不是,则仅显示该单元格。

我该怎么做呢?我正在使用 SqlDataSource 来选择 GridView 的内容,但我愿意将其更改为更具编程性的方法。

谢谢

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    我不确定我是否理解正确,但如果你愿意,你可以自己建立你的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);
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-13
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      相关资源
      最近更新 更多