如何通过GridView绑定数据源的列的一部分? [英] How to bind one part of columns of a DataSource by GridView?
问题描述
我不希望将数据源的所有列绑定到GridView上,而是将其中的一部分绑定到GridView上,但是我不知道可以使用哪个函数?
I want to bind not all but one part of columns of a DataSource to a GridView, but I do not know which function can be used?
例如,以下代码用于将数据库"Departments
"的所有列绑定到GridView departmentsGrid
.
For example, the following code is used to bind all columns of database "Departments
" to GridView departmentsGrid
.
departmentsGrid.DataSource = dataSet.Tables["Departments"].DefaultView;
departmentsGrid.DataBind();
请注意,对于GridView,我已将"AutoGenerateColumns
"设置为True.
Note that for the GridView, I have set "AutoGenerateColumns
" as True.
因此,我必须选择所有要绑定的列.也许,我可以将"AutoGenerateColumns
"设置为False,然后一一绑定列?
Therefore, I have to select all the columns to bind. Perhaps, I can set "AutoGenerateColumns
" as False and then bind the columns one by one?
还是有更好的解决方案?
Or is there any better solution?
谢谢!
推荐答案
您必须在Gridview的RowDataBound
事件上进行此操作.
You have to do it onRowDataBound
Event of Gridview.
总是最好在设计器上指定列,而AutoGenerateColumns设置为false.这样,如果对部门的选择发生更改,页面将保持不变.
[竖起大拇指]
It is always better to specify columns on the designer and AutoGenerateColumns false. So that if the selection to departments changes the page becomes intact.
[thumbs up]
这篇关于如何通过GridView绑定数据源的列的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!