display lookup data as text in asp.net gridview with microsoft access backend
i am facing a开发者_如何学Go boring issue when i have to present lookup table data (with MS Access backend) in an ASP.net page
the main table Employees that the asp.net page will show its data includes one field main.gender with type "Integer" which is linked to the lookup table gender
if the table is linked to gridview, then the field in question will display
"1" or "2"
i need to display
"male" and "female"
i figured out two ways to solve this issue but i dont think any of them is the best thing to do
first way:
-convert the gridview field gender to template field, replace the label in edit template with dropdownlist linked to the lookup table and display gender as text -make the DDL enabled=false to be "read only" -problem is that DDL show dimmed text
second way:
to make a select query replacing Employee.gender(integer) with gender.gender(text) this way is MUCH SLOWER in data retrieval
could anyone please help me find a way to display text with a more "professional" way
You can create a Enum with your Data:
protected enum MyEnum
{
male = 1,
female = 2
}
And then use something like this:
<asp:GridView runat="server" ID="gridView1" AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="PropertyName" />
//Add one column for your each property you want to display
<asp:TemplateField HeaderText="Gender">
<ItemTemplate>
<%# (MyEnum)Eval("Gender") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Hope it will be helpful.
Mokokamello
try use join in the asp.net datasource select command, below is how it looks like in pseudocode
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/database.mdb"
SelectCommand="SELECT Table1.ID, Table1.NameEmployee, Gender.Gender FROM Table1 INNER JOIN Gender ON Table1.gender = Gender.ID">
</asp:AccessDataSource>
it need no code behind, no dropdown lists, no database embedded queries and it is significantly faster that the ways you mentioned
精彩评论