Reputation: 9017
I have two classes:
public class Equipment
{
public int EquipmentId { get; set; }
public string Name { get; set; }
public Nullable<int> EquipmentTypeId { get; set; }
}
public class EquipmentType
{
public int EquipmentTypeId { get; set; }
public string TypeName { get; set; }
}
I'm returning list of Equipment from database as a List<Equipment>
.
Then I bind it to the Gridview through Gridview1.DataSource = equipmentList
That works fine, but what I need to do is I need to display EquipmentType Name instead of ID. And, in edit mode I need to display Dropdown list with all available EQuipmentTypeNames. Any suggestion on how to do that? what is the architecture?
Upvotes: 0
Views: 115
Reputation: 56769
For displaying in the GridView you have a couple of options, ranging from easiest to hardest.
Here is an example of how you could include the equipment type data:
var equips = GetEquipment();
var equipTypes = GetEquipmentTypes();
var data = from eq in equips
join et in equipTypes on eq.EquipmentTypeId equals et.EquipmentTypeId
select new { eq.EquipmentId, eq.Name, et.TypeName };
GridView.DataSource = data;
For editing in the GridView, it is simplest just to use a DropDownList with it's own SqlDataSource (or custom data source), which is bound to Equipment.EquipmentTypeId
, something like this:
<EditItemTemplate>
<asp:DropDownList ID="EquipmentTypeDropDown" runat="server"
SelectedValue='<%#Bind("EquipmentTypeId") %>'
DataSource='<%#GetEquipmentTypes() %>'
DataTextField="TypeName" DataValueField="EquipmentTypeId">
</asp:DropDownList>
</EditItemTemplate>
Where GetEquipmentTypes()
is just a method that returns List<EquipmentType>
that I'm using for databinding. In this case, since I don't have a database, I'm just generating data in a stub object. You would replace this method with your BAL class that reads the data from the database (or ADO.Net code, or an ObjectDataSource, etc.).
protected List<EquipmentType> GetEquipmentTypes()
{
return new List<EquipmentType> {
new EquipmentType { EquipmentTypeId = 1, TypeName = "Baseball" },
new EquipmentType { EquipmentTypeId = 2, TypeName = "Football" },
new EquipmentType { EquipmentTypeId = 3, TypeName = "Soccer" },
};
}
Upvotes: 1
Reputation: 237
First of all , if you want to bind it like that you need to make your own class that contains all informations , like that:
public class CompositeEquipment
{
public int EquipmentId { get; set; }
public string Name { get; set; }
public string EquipmentTypeName { get; set; }
}
And if you are using sql statements or stored procedures you can write the following :
SELECT Equipment.EquipmentID,Equipment.Name,EquipmentType.TypeName FROM Equipment
INNER JOIN EquipmentType ON Equipment.EquipmentTypeId = EquipmentType.EquipmentTypeId
and when reading SqlDataReader do the following :
List<Equipment> lstEquipment = new List<Equipment>();
while(reader.read())
{
Equipment eq = new Equipment();
eq.reader["EquipmentId"];
eq.reader["Name"];
eq.reader["TypeName "];
lstEquipment.Add(eq);
}
Gridview1.DataSource = lstEquipment;
and if you are using EntityFramework as Data Access Layer you can write the following :
using(YourOwnContext context = new YourOwnContext)
{
List<Equipment> lstEquipment = (from e in context.Equipments
select new Equipment
{
EquipmentId = e.EquipmentId,
Name = e.Name,
TypeName = e.EquipmentType.TypeName
}}).ToList();
Gridview1.DataSource = lstEquipment;
Hope this helps you.
Upvotes: 0
Reputation: 40393
I'd recommend extending your Equipment
class out with a child class (let's call it EquipmentEx
for now), and add on the EquipmentTypeName
property. Instead of your stored proc returning just the Equipment
columns, join in the EquipmentType
table, and include the additional type name column.
Or create a view that does the join, and have your stored proc return the view contents instead of the table contents - it depends on how your app is generally organized.
Once you have an EquipmentEx
object, then you've got your answer for displaying the type name instead of ID - it's already part of the object, so you can display it just like normal.
As far as putting the dropdown into the GridView, you'll probably be working with the EditItemTemplate tag inside your template field, but I really haven't worked enough with the GridView to know exactly what it will look like. I've always preferred to handle this kind of thing a little more manually, rather than trusting ASP.NET to do it for me. But since your dropdownlist's value will represent the EquipmentTypeID, you should be able to bind this value to the object when you save it, instead of a TextBox value.
Upvotes: 1