我有一个列表,其中包含我想在gridview上显示的所有行。我从数据层上的datareader获取这些行,一旦我将列表调用到表示层,我就想将列表结果绑定到gridview。有没有办法做到这一点?代码如下。
public class SubmissionDatabaseService
{
public List<Submission> GetAllSubmissions()
{
string Searchstring = "SELECT Submission.SubmissionId, Customer.CustName, Customer.CustCity, Customer.CustState, Broker.BroName, Broker.BroState, Broker.EntityType, Submission.Coverage, Status.Status FROM Submission INNER JOIN Broker ON Broker.BroId = Submission.BroId INNER JOIN Customer ON Customer.CustId = Submission.CustId INNER JOIN Status ON Status.StatusId = Submission.StatusId";
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
SqlDataReader dr = null;
List<Submission> lstSubmission = new List<Submission>();
conn.Open();
SqlCommand Searchcmd = new SqlCommand(Searchstring, conn);
dr = Searchcmd.ExecuteReader();
Submission tempSubmission;
while (dr.Read())
{
tempSubmission = new Submission();
tempSubmission.setSubmissionId(dr.GetInt32(0));
tempSubmission._Cust.setCustName(dr.GetString(1));
tempSubmission._Cust.setCustCity(dr.GetString(2));
tempSubmission._Cust.setCustState(dr.GetString(3));
tempSubmission._Bro.setBroName(dr.GetString(4));
tempSubmission._Bro.setBroState(dr.GetString(5));
tempSubmission._Bro.setEntity(dr.GetString(6));
tempSubmission._SubCov.setCoverage(dr.GetInt32(7));
tempSubmission.setStatus(dr.GetString(8));
lstSubmission.Add(tempSubmission);
}
return lstSubmission;
}
}
public class SubmissionService
{
public SubmissionService() { }
private Array _getllsubservice;
public Array Getallsubservice
{
get { return _getllsubservice; }
set { _getllsubservice = value; }
}
public List<Submission> getAllSubmission()
{
SubmissionDatabaseService sds = new SubmissionDatabaseService();
return sds.GetAllSubmissions();
}
}
SubmissionService ss = new SubmissionService();
List<Submission> SubmissionsList = ss.getAllSubmission();`
答案 0 :(得分:4)
要将数据加载到gridview中,请尝试:
yourgrid.DataSource=SubmissionsList;
yourgrid.DataBind();
这里有用的东西:
How to Bind List<myclass> data with gridview?
或在这里: