如何将SQL表中某一列的值作为GridView的header

时间:2021-11-07 01:39:19

如何将SQL表中某一列的值作为GridView的header

I have SQL database table as shown in the image. I want values of column Location_Instance(i.e., the second column in the image) to to displayed as headers of a GridView. Right now I am copying the column the to a ListBox and using the ListBox to be displayed as headers. But I want to display directly from datasource. Can anyone kindly help me on this. Thank you in advance.

我有SQL数据库表,如图所示。我需要列Location_Instance的值。显示为GridView的头部。现在,我正在将列复制到列表框中,并使用列表框作为标题显示。但是我想直接从数据源中显示。在这件事上谁能帮我一个忙吗?提前谢谢你。

4 个解决方案

#1


1  

One way to do this would be to write some additional code that creates a new DataTable with the columns set how you want, then insert rows how you want to, from the data in the database.

这样做的一个方法是编写一些附加的代码,这些代码创建一个新的DataTable,其中的列设置了您想要的,然后从数据库中的数据插入您想要的行。

Doing it in SQL is difficult.

用SQL进行操作是很困难的。

See http://msdn.microsoft.com/en-us/library/hfx3s9wd.aspx for how to do this ...

如何做到这一点,请参阅http://msdn.microsoft.com/en-us/library/hfx3s9wd.aspx。

#2


0  

You can not directly set the column's value as header in grid view, first you have to save it at some place eg ViewState or any control

不能在grid view中直接将列的值设置为header,首先必须将其保存到某个位置(如ViewState或任何控件)

#3


0  

Write a Select Query and Get the data

编写一个Select查询并获取数据

like

就像

Select * from Tbl where 'Condition'

Fill the data in a datatable (ex: dt). Then bind data directly to the gridview. like

将数据填充到一个datatable中(例如:dt)。然后将数据直接绑定到gridview。就像

Gridview.Datasource = dt; Gridview.Databind();

显示数据表格。数据源= dt;Gridview.Databind();

Then Directly The headers will be displayed.

然后直接显示标题。

If you want to Display Location_Type as Location Type then you have to write SQL Query like this

如果您想要显示Location_Type作为位置类型,那么您必须编写这样的SQL查询。

Select Location_Type as [Location Type] .... from Tbl where 'Condition'

#4


0  

Logically this is not possible. Because in this case you want to show rows of data in a single row with multiple column. SO in case your location instance increases what will you do??

从逻辑上讲,这是不可能的。因为在这种情况下,您希望显示一行中有多个列的数据行。如果你的位置实例增加了,你会怎么做?

But if the number of location_instance is fixed then there is a work around. But this is also limited. You can show the location instance as grid view row, not as column. You can style the row so that it will look like header.

但是如果location_instance的数量是固定的,那么就有一个工作。但这也是有限的。可以将位置实例显示为网格视图行,而不是列。可以对行进行样式化,使其看起来像header。

To know how to convert a column data to a row data check the below link. http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/

要知道如何将列数据转换为行数据,请查看下面的链接。http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/

So once you get the data in a row you can bind the data to the grid view easily by using RowDataBound of grid view.

因此,一旦获取了行中的数据,就可以通过使用网格视图的RowDataBound来轻松地将数据绑定到网格视图。

#1


1  

One way to do this would be to write some additional code that creates a new DataTable with the columns set how you want, then insert rows how you want to, from the data in the database.

这样做的一个方法是编写一些附加的代码,这些代码创建一个新的DataTable,其中的列设置了您想要的,然后从数据库中的数据插入您想要的行。

Doing it in SQL is difficult.

用SQL进行操作是很困难的。

See http://msdn.microsoft.com/en-us/library/hfx3s9wd.aspx for how to do this ...

如何做到这一点,请参阅http://msdn.microsoft.com/en-us/library/hfx3s9wd.aspx。

#2


0  

You can not directly set the column's value as header in grid view, first you have to save it at some place eg ViewState or any control

不能在grid view中直接将列的值设置为header,首先必须将其保存到某个位置(如ViewState或任何控件)

#3


0  

Write a Select Query and Get the data

编写一个Select查询并获取数据

like

就像

Select * from Tbl where 'Condition'

Fill the data in a datatable (ex: dt). Then bind data directly to the gridview. like

将数据填充到一个datatable中(例如:dt)。然后将数据直接绑定到gridview。就像

Gridview.Datasource = dt; Gridview.Databind();

显示数据表格。数据源= dt;Gridview.Databind();

Then Directly The headers will be displayed.

然后直接显示标题。

If you want to Display Location_Type as Location Type then you have to write SQL Query like this

如果您想要显示Location_Type作为位置类型,那么您必须编写这样的SQL查询。

Select Location_Type as [Location Type] .... from Tbl where 'Condition'

#4


0  

Logically this is not possible. Because in this case you want to show rows of data in a single row with multiple column. SO in case your location instance increases what will you do??

从逻辑上讲,这是不可能的。因为在这种情况下,您希望显示一行中有多个列的数据行。如果你的位置实例增加了,你会怎么做?

But if the number of location_instance is fixed then there is a work around. But this is also limited. You can show the location instance as grid view row, not as column. You can style the row so that it will look like header.

但是如果location_instance的数量是固定的,那么就有一个工作。但这也是有限的。可以将位置实例显示为网格视图行,而不是列。可以对行进行样式化,使其看起来像header。

To know how to convert a column data to a row data check the below link. http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/

要知道如何将列数据转换为行数据,请查看下面的链接。http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/

So once you get the data in a row you can bind the data to the grid view easily by using RowDataBound of grid view.

因此,一旦获取了行中的数据,就可以通过使用网格视图的RowDataBound来轻松地将数据绑定到网格视图。