How can i show Data from a class into a gridview? MY Class is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class EmployeeDetails
{
private int employeeID;
public int EmployeeID
{
get
{
return employeeID;
}
set
{
employeeID =开发者_StackOverflow中文版 value;
}
}
private string firstName;
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}
private string lastName;
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}
private string titleOfCourtesy;
public string TitleOfCourtesy
{
get
{
return titleOfCourtesy;
}
set
{
titleOfCourtesy = value;
}
}
public EmployeeDetails(int employeeID, string firstName, string lastName, string titleOfCourtesy)
{
EmployeeID = employeeID;
FirstName = firstName;
LastName = lastName;
TitleOfCourtesy = titleOfCourtesy;
}
}
I`ve done this:
protected void Page_Load(object sender, EventArgs e)
{
int id = 15;
string f_name = "asd";
string l_name = "asd";
string title = "asd";
EmployeeDetails emp = new EmployeeDetails(id,f_name,l_name,title);
emp.EmployeeID = id;
emp.FirstName = f_name;
emp.LastName = l_name;
emp.TitleOfCourtesy = title;
}
List<EmployeeDetails> lst = new List<EmployeeDetails>() ;
GridView1.DataSource = lst ;
GrdiView1.DataBind();
you may populate the list with your EmployeeDetails
Objects
A GridView
is generally used to display multiple objects/rows. To get it to display your class you need to make a collection containing your class, such as a List<EmployeeDetails>
. Then bind that to your gridview.
You could use another control more suited to displaying a single object such as a DetailsView
.
I assume you are looking for an answer to show multiple item instead of just 1.
C#
var myList = List<EmploymentDetails>();
foreach (var employee in SomeOtherDataSource)
{
EmployeeDetails emp = new EmployeeDetails{EmployeeID = id, FirstName = f_name, LastName = l_name, TitleOfCourtesy = title};
myList.Add(emp);
}
var EmployeeDS = from eds in myList select new { ID = EmployeeID, FName = FirstName, LName = LastName, Title = TitleofCourtest };
MyGridview.DataSource = EmployeeDS;
MyGridView.DataBind();
By doing this: var EmployeeDS = from eds in myList select new { ID = EmployeeID, FName = FirstName, LName = LastName, Title = TitleofCourtest };
it makes it possible to just use <%# Eval('ID/FName/LName/Title'%>
in the GridView's boundfields instead of those long variable names you've made in your entity class.
精彩评论