Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jul 16, 2012 03:15 PM by superguppie
Jun 30, 2012 01:52 PM|LINK
I want to display result set returned by Stored Procedure on Gridview.
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.
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:
GridView1.DataSource = result; //here result is a datatable.
then you can change the column name by changing the headertext like
Gridview1.Columns.HeaderText = "My Header Text";
I think giving the appropriate names to the columns in the database itself would be a better solution.
Jun 30, 2012 02:16 PM|LINK
whatever u have mentioned is just databinding part .
is anyone knows solution of this problem. completely
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.
Jul 12, 2012 04:47 AM|LINK
Thank for this approach..
Can i have some code sample for this.
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";