I want to display result set returned by Stored Procedure on Gridview.
Requirement:
1. Stored Procedure retured table in which no. of columns may vary. (For ex. Some time 5, sometime 20 columns.)
2. Table returned by SP having some columns name like A, AB , AC (going to be header in gridview)which needs to be given meaningful name in .net CodeBehind.
GridView can generate the columns for you. It does so by default. If you have AutoGenerateColumns="false" on the GridView, then take it off.
Replacing the column headers with meaningful names is probably easiest in the RowDataBound handler. In that, you can find the Row in the event arguments. Check if RowType is Header. When it is, loop over the Cells collection. If a Cell is a DataControlFieldCell,
cast to that and you can get its ContainingField. If that is a BoundField, cast to that and you can get its DataField. Use that to find the meaningful name, and set that on the Text of the Cell.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
Marked as answer by Dino He - MSFT on Jul 06, 2012 05:33 AM
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
AshwaniVishw...
Member
22 Points
12 Posts
When number of columns are not fixed in GridView
Jun 30, 2012 01:52 PM|LINK
Hi,
I want to display result set returned by Stored Procedure on Gridview.
Requirement:
1. Stored Procedure retured table in which no. of columns may vary. (For ex. Some time 5, sometime 20 columns.)
2. Table returned by SP having some columns name like A, AB , AC (going to be header in gridview)which needs to be given meaningful name in .net CodeBehind.
Please help to resolve this.
Ashwani
arketes
Member
4 Points
5 Posts
Re: When number of columns are not fixed in GridView
Jun 30, 2012 02:11 PM|LINK
obtain the result of stored procedure in a DataTable.
bind it to the gridview.
this is done by:
then you can change the column name by changing the headertext like
I think giving the appropriate names to the columns in the database itself would be a better solution.
AshwaniVishw...
Member
22 Points
12 Posts
Re: When number of columns are not fixed in GridView
Jun 30, 2012 02:16 PM|LINK
Dear,
whatever u have mentioned is just databinding part .
..
is anyone knows solution of this problem. completely
Ashwani
superguppie
All-Star
48225 Points
8679 Posts
Re: When number of columns are not fixed in GridView
Jul 04, 2012 10:29 AM|LINK
GridView can generate the columns for you. It does so by default. If you have AutoGenerateColumns="false" on the GridView, then take it off.
Replacing the column headers with meaningful names is probably easiest in the RowDataBound handler. In that, you can find the Row in the event arguments. Check if RowType is Header. When it is, loop over the Cells collection. If a Cell is a DataControlFieldCell, cast to that and you can get its ContainingField. If that is a BoundField, cast to that and you can get its DataField. Use that to find the meaningful name, and set that on the Text of the Cell.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
AshwaniVishw...
Member
22 Points
12 Posts
Re: When number of columns are not fixed in GridView
Jul 12, 2012 04:47 AM|LINK
Hi,
Thank for this approach..
Can i have some code sample for this.
Ashwani
superguppie
All-Star
48225 Points
8679 Posts
Re: When number of columns are not fixed in GridView
Jul 16, 2012 03:15 PM|LINK
Without testing, I was thinking something like
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if(e.Row.RowType == DataControlRowType.Header) { foreach(TableCell cell in e.Row.Cells) { if(cell is DataControlFieldCell) { DataControlFieldCell dataControlFieldCell = (DataControlFieldCell)cell; if(dataControlFieldCell.ContainingField is BoundField) { BoundField boundField = (BoundField)dataControlFieldCell.ContainingField; if(boundField.DataField == "FamilyName") { cell.Text = "Family"; } } } } } }Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.