Reputation: 407
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.
Upvotes: 0
Views: 993
Reputation: 680
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.
Doing it in SQL is difficult.
See http://msdn.microsoft.com/en-us/library/hfx3s9wd.aspx for how to do this ...
Upvotes: 1
Reputation: 3117
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.
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/
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.
Upvotes: 0
Reputation: 7026
Write a Select Query and Get the data
like
Select * from Tbl where 'Condition'
Fill the data in a datatable (ex: dt). Then bind data directly to the gridview. like
Gridview.Datasource = 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
Select Location_Type as [Location Type] .... from Tbl where 'Condition'
Upvotes: 0
Reputation: 6123
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
Upvotes: 0