【问题标题】:How can I hide empty columns in a gridview without knowing which will be empty?如何在不知道哪个为空的情况下隐藏网格视图中的空列?
【发布时间】:2011-03-04 15:44:20
【问题描述】:

我正在使用一个网格视图,它根据下拉列表中的选择从 SQL 数据库中提取数据。源表有六列用于选择的属性,但根据选择的内容,可能有 1 到 6 个为空的列(全为空值)。当该列为空时,我希望将其隐藏,以使页面不那么笨重和混乱。

过去几天我一直在寻找答案,但到目前为止我发现的要么与隐藏你知道是空的列有关,我不知道,要么在我认为的 SQL 代码中删除它们如果在 gridview 代码中调用了该列并且查询中不存在该列,则该列不起作用。

我是 ASP.NET 的新手,所以如果我的某些术语有问题,我很抱歉!如果您对我的问题有任何疑问,请告诉我。

提前感谢您的帮助!

【问题讨论】:

  • 请提供进一步的说明 - 您使用的是哪个版本的 .Net 框架和 Visual Studio?这将影响答案,因为 Linq 在 .Net 3.5 (VS 2008) 之前不可用。此外,代码正在做什么的示例将有助于提供答案。
  • 能不能不让它加载,然后检查是否有没有值的列

标签: c# asp.net gridview


【解决方案1】:

与其隐藏空列,不如在代码后面添加所需的列

当您检索要显示的数据时,您知道存在哪些列。您可以在后面的代码中添加它们并进行数据绑定。

为了让您开始使用,这里有一些来自helpful article 的代码,说明如何执行此操作:

BoundField nameColumn = new BoundField();
nameColumn.DataField = "Name";
nameColumn.HeaderText = "Person Name";
GridView1.Columns.Add(nameColumn);

【讨论】:

    【解决方案2】:

    你可以试试:

    1. 设置gridview将所有列隐藏(Visible="false")
    2. 在 gridview 的 RowDataBound 事件中,检查每个列的值,如果有数据,则将列设置为显示 (Visible="true")

    RowDataBound 方法可能如下所示:

    void YourGridview_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // if this is a SqlDataSource you can use DataRowView, 
            // otherwise use whatever type is used in the data source
            DataRowView rowView = (DataRowView)e.Row.DataItem;
    
            if (!String.IsNullOrEmpty(rowView["ColumnA"])) 
                YourGridview.Columns[0].Visible = true;
            // repeat for your other columns
        }
    }
    

    【讨论】:

    • 如何检查每一列是否有数据?你有任何示例代码吗?谢谢!
    • 我试过这段代码,但它给了我一个错误。我将 DataRowView 更改为 System.Data.DataRowView,但如果没有收到消息“'string.IsNullOrEmpty(string)' 的最佳重载方法匹配有一些无效参数”,我无法弄清楚将 rowView 更改为什么。您对我如何解决这个问题有任何想法吗?
    • @Kate,我需要有关您尝试使用的数据集/类型的更多信息。
    • @Forgotten Semicolon,我正在使用数据类型为 varchar 和 float 的 SQL 表(我希望将其应用于每个列的几列)。我正在使用 VisualStudio2010 并使用 C# 进行编码。您还需要其他信息吗?感谢您的帮助!
    • @Kate,如果浮动字段可以为空,您应该能够像这样检查它们: if (myFloatField.HasValue) YourGridview.Columns[0].Visible = true;
    【解决方案3】:

    为网络做到这一点的最佳方式:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI.WebControls;
    
    namespace MyProject
    {
        public static class ExtensionGridView
        {
            public static GridView RemoveEmptyColumns(this GridView gv)
            {
                // Make sure there are at least header row
                if (gv.HeaderRow != null)
                {
                    int columnIndex = 0;
    
                    // For each column
                    foreach (DataControlFieldCell clm in gv.HeaderRow.Cells)
                    {
                        bool notAvailable = true;
    
                        // For each row
                        foreach (GridViewRow row in gv.Rows)
                        {
                            string columnData = row.Cells[columnIndex].Text;
                            if (!(string.IsNullOrEmpty(columnData) || columnData ==" "))
                            {
                                notAvailable = false;
                                break;
                            }
                        }
    
                        if (notAvailable)
                        {
                            // Hide the target header cell
                            gv.HeaderRow.Cells[columnIndex].Visible = false;
    
                            // Hide the target cell of each row
                            foreach (GridViewRow row in gv.Rows)
                                row.Cells[columnIndex].Visible = false;
                        }
    
                        columnIndex++;
                    }
                }
    
                return gv;
            }
        }
    }
    

    【讨论】:

    • 谢谢 - 我使用了这种方法。它比其他解决方案干净得多。
    • 我已经在我的 DataGridView 的“PreRender”事件中使用了它 - 作为一个魅力。
    【解决方案4】:

    代替这段代码:

    if (!String.IsNullOrEmpty(rowView["ColumnA"])) 
                YourGridview.Columns[0].Visible = true;
    

    使用这个:

    if (!String.IsNullOrEmpty(Convert.Tostring(rowView["ColumnA"])) )
                YourGridview.Columns[0].Visible = true;
    

    【讨论】:

      【解决方案5】:

      在网格的onItemBound事件上,可以检查列单元格中要设置的变量的值,并相应地设置其可见性

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-06
        • 2017-11-14
        • 2014-04-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多