如何在gridview中隐藏空列 [英] how to hide empty columns in gridview

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

问题描述

如何在gridview中隐藏空值或空值列。

how can i hide empty or null value columns in gridview.

推荐答案

通过在绑定之前处理数据来绑定非空列数据。例如,如果从sql server将数据加载到DataTable,则可以删除DataTable中的空列(请参阅:从DataTable中删除没有数据的所有列 [ ^ ])并将其绑定为数据源。
bind the non empty column data by processing data before binding. for example if you load the data to a DataTable from the sql server, you can remove empty column in the DataTable ( refer : Remove all columns with no data from DataTable[^]) and bind it as data source.


你可以做的是检查在GridView的RowDataBound()事件中为NULL或为空的列,并相应地隐藏列。在事件方法中首先检查DataRow,然后重复其他列



What you can do is check the columns for NULL or empty in RowDataBound() event of GridView and hide the columns accordingly. In the event method check first for DataRow like this and repeat for other columns

if (e.Row.RowType == DataControlRowType.DataRow)
            {
               // To hide the first column
               string vToCheck = e.Row.Cells[0].ToString();
               if(string.IsNullorEmpty(vToCheck) )
                {
                gridView.Columns[0].Visible = false;     
                }

            }


请参阅:

https://msdn.microsoft.com/en-us /library/system.web.ui.webcontrols.datacontrolfield.visible%28v=vs.110%29.aspx [ ^ ],

< a href =https://msdn.microsoft.com/en-us/library/system.web.ui.control.visible(v=vs.110).aspx> https://msdn.microsoft.com/ en-us / library / system.web.ui.control.visible(v = vs.110).aspx [ ^ ]。



如您所见,您可以更改 DataControlField 或任何 Control 的任何实例的可见性,包括的TableCell 。然后看看表的结构。它有控件属性,单元格是行集合的元素。根据您的情况扫描问题列中的所有数据,显示或隐藏您想要的任何内容。



-SA
Please see:
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datacontrolfield.visible%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.web.ui.control.visible(v=vs.110).aspx[^].

As you can see, you can change visibility of any instance of DataControlField or any Control, including TableCell. Then look at the structure of the table. It has Rows and Controls properties, and the cells are the elements of rows collection. Scan all data in columns in questions, show or hide whatever you want depending on your condition.

—SA


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

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