So here is the scenario. I need to pull out headertext from database for my gridview. I am going to give the column name of the given fields that I want from the database becuase I dont need to show all the columns from database to gridview. Here is what
I have got so far. Here is aspx code.
Is there any way where I can specify the column name I want from database and get the text value of it since I dont want to hardcode it at all. My code will pass the value like "ACTIVE_YN" and is going to match up in a dictionary (which is in database) and
return me a user friendly description to the value I pass. Please all help is greatly appreciated. Thanks and have a great day :)
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.Header Then
e.Row.Cells("Your Column Number").Text = "Your Header Text"
End If
End Sub
I would send a second table with four columns. One column for the name of the gridView that you are applying the data to, one for the database column name, one column for the client column name and the last column to flag a column for delete. So how this
works is that you select the gridView name from the second table (you might have more than one grid View) to return all of the rows (rows in this case are really the columns do display). Now you have several options. If you don't want to keep the data for
anything else (like binding to another gridView or a lookup) you can loop over the dataTable with your selected rows and update the first dataTable with the client column name you wish to use and remove the columns you do not wish to display and the bind.
Option two (if you wish to preserve the data) is to make a copy of the original dataTable and then perform the above actions on this copy of the data. Option three (if you want to keep the data) is to bind your gridView to the original dataTable and then using
the second table loop over your gridView and change the header names and either delete or change the visibility of the columns you wish to hide. BTW, you could add this to a base class and have a generic/easy way to maintain your gridViews.
HereToLearn_
0 Points
1 Post
How to pull out headertext for gridview from database?
Dec 03, 2012 04:09 PM|LINK
So here is the scenario. I need to pull out headertext from database for my gridview. I am going to give the column name of the given fields that I want from the database becuase I dont need to show all the columns from database to gridview. Here is what I have got so far. Here is aspx code.
<asp:GridView ID="gvCustProfile" runat="server" AllowPaging="True" DataKeyNames="custname" AutoGenerateColumns="False"
BorderStyle="None" GridLines="Horizontal" HeaderStyle-HorizontalAlign="Left"
ShowFooter="True" Width="125%" AllowSorting="True">
Here is what I got in CS file.
gvCustProfile.Columns["ACTIVE_YN"].HeaderText = GetColNameFromDictionary(inputColName, profileTable);
Is there any way where I can specify the column name I want from database and get the text value of it since I dont want to hardcode it at all. My code will pass the value like "ACTIVE_YN" and is going to match up in a dictionary (which is in database) and return me a user friendly description to the value I pass. Please all help is greatly appreciated. Thanks and have a great day :)
nganesha
Member
46 Points
51 Posts
Re: How to pull out headertext for gridview from database?
Dec 03, 2012 08:13 PM|LINK
In Gridview Rowdatabound event you can try this:
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound If e.Row.RowType = DataControlRowType.Header Then e.Row.Cells("Your Column Number").Text = "Your Header Text" End If End SubSean Gahan
Member
85 Points
14 Posts
Re: How to pull out headertext for gridview from database?
Dec 03, 2012 10:21 PM|LINK
I would send a second table with four columns. One column for the name of the gridView that you are applying the data to, one for the database column name, one column for the client column name and the last column to flag a column for delete. So how this works is that you select the gridView name from the second table (you might have more than one grid View) to return all of the rows (rows in this case are really the columns do display). Now you have several options. If you don't want to keep the data for anything else (like binding to another gridView or a lookup) you can loop over the dataTable with your selected rows and update the first dataTable with the client column name you wish to use and remove the columns you do not wish to display and the bind. Option two (if you wish to preserve the data) is to make a copy of the original dataTable and then perform the above actions on this copy of the data. Option three (if you want to keep the data) is to bind your gridView to the original dataTable and then using the second table loop over your gridView and change the header names and either delete or change the visibility of the columns you wish to hide. BTW, you could add this to a base class and have a generic/easy way to maintain your gridViews.
Let me know if this is not clear.
Sean Gahan
http://SeanGahan.Net