Click here to Skip to main content
16,022,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can we read DataField of a bound field column for a GridView in the asp.net application?

I want to create a data table at run time for any grid.

public static void fnSetBlankGridMessage(GridView gv, string strMessage)
{
.
.
.
}

Following is my aspx code

C#
<asp:BoundField DataField="Service_Number" HeaderText="Service">
<HeaderStyle CssClass="grdheader" />


I want to read "Service_Number" at runtime.
Posted
Updated 19-Oct-11 21:44pm
v2

1 solution

Hi AmolPandurangBorkar,

Please try the solution given below. It may help you to read DataField property of a bound column for a GridView.

C#
System.Text.StringBuilder sbBoundFieldNames = new System.Text.StringBuilder();

foreach (System.Web.UI.WebControls.BoundField bfName in this.GridView1.Columns)
{
    sbBoundFieldNames.Append("<br />" + bfName.DataField);
}

Response.Write(sbBoundFieldNames.ToString());


Regards,
Ambicaprasad Maurya
 
Share this answer
 
Comments
Amol_27101982, India 25-Oct-11 3:21am    
Thanks a lot Ambicaprasad Maurya...
You put me very close to my solution.
By doing some modifications I am able to achieve my goal.

Also as I have "TemplateField"s in the grid, I made following change

public static void fnSetBlankGridMessage(GridView gv, string strMessage)
{
System.Text.StringBuilder sbBoundFieldNames = new System.Text.StringBuilder();
System.Web.UI.WebControls.BoundField bfName;
for (int intCnt = 0; intCnt < gv.Columns.Count; intCnt++)
{
if (gv.Columns[intCnt].GetType().Name == "BoundField")
{
bfName = (System.Web.UI.WebControls.BoundField)gv.Columns[intCnt];
sbBoundFieldNames.Append("<br />" + bfName.DataField);
}
}
Response.Write(sbBoundFieldNames.ToString());

}


Thanks again
~Amol
madhavi dusari 11-Dec-15 0:32am    
hi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900