如何隐藏在GridView空列不知道这将是空的? [英] How can I hide empty columns in a gridview without knowing which will be empty?

查看:106
本文介绍了如何隐藏在GridView空列不知道这将是空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与基于在下拉列表中选择一个SQL数据库中提取数据的GridView工作。源表具有用于选择的属性六列,但根据选择什么,有可能是从一个任意位置那些空的(所有空值)中的六个。当列是空的,我想有它藏了这么页面不太笨重和混乱。

I am working with a gridview that pulls data from a SQL database based on selections in dropdown lists. The source table has six columns for attributes of the selection, but depending on what is chosen, there could be anywhere from one to six of those that are empty (all null values). When the column is empty, I would like to have it hidden so the page is less clunky and confusing.

我搜索周围,在过去几天的答案,但是到目前为止,我发现要么是相关的隐藏,你知道是空的,我不知道,或在SQL code删除它们列我认为不起作用如果列在GridView code呼吁,并在查询不存在的。

I've searched around for an answer for the past couple days, but what I have found so far is either related to hiding columns that you know are empty which I will not know or removing them in the SQL code which I think doesn't work if the column is called for in the gridview code and doesn't exist in the query.

我很新的ASP.NET,所以我很抱歉,如果我的一些术语是关闭!我们如果您有关于要问我任何问题,我知道了。

I'm very new to ASP.NET, so I'm sorry if some of my terminology is off! Let me know if you have any questions about what I'm asking.

在此先感谢您的帮助!

推荐答案

您可以尝试:


  1. 设置了GridView的将所有列隐藏(可见=假

  2. 在GridView的RowDataBound事件,检查每列值,如果它有数据,设置栏,显示(可见=真

  1. Set up the gridview will all columns hidden (Visible="false")
  2. In the gridview's RowDataBound event, check each of the column values and if it has data, set the column to show (Visible="true")

该方法的RowDataBound可能是这个样子:

The RowDataBound method could look something like this:

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

这篇关于如何隐藏在GridView空列不知道这将是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆