简体   繁体   English

动态更改GridView行(ASP.NET,C#)

[英]Dynamically changing GridView row (ASP.NET,C#)

I have a GridView that needs to display either x amount of columns from the GridView, or just one column (cell) which will expand to take up the entire row. 我有一个GridView,它需要显示GridView的x列数,或者仅显示一列(单元格),它将扩展以占据整个行。 Deciding which to display is done by checking if that one column is blank or not - if it is blank, display the other cells, or if not, just display that one cell. 通过检查一列是否为空白来确定要显示的内容-如果该列为空白,则显示其他单元格;否则,仅显示该单元格。

How would I go about doing this? 我将如何去做呢? I'm using an SqlDataSource to select the contents of the GridView but I'm willing to change this for a more programmatic approach. 我正在使用SqlDataSource选择GridView的内容,但我愿意更改它以采用更具编程性的方法。

Thanks 谢谢

I'm not sure if I understand you correctly, but if you want you can build your Table yourself. 我不确定我是否理解正确,但是如果您愿意,可以自己构建Table Then you can control exactly what columns (and what data) to include. 然后,您可以精确控制要包括的列(和数据)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM