如何将List绑定到gridview [英] How to bind List to gridview
本文介绍了如何将List绑定到gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我试图通过绑定List来显示gridview上的记录,它的最后一条记录是覆盖其他记录!这是我的代码:
Hello everyone,
i am trying to display record on gridview by binding List to it, it the last record is overwriting other records! this is my code:
public class BVNNotEnrolledAllBranches
{
public string CustomerName { get; set; }
public string AccountNumber { get; set; }
public string CIF { get; set; }
public string PhoneNumber { get; set; }
public string BranchName { get; set; }
public string BVN { get; set; }
public string Email { get; set; }
}
using (OracleConnection connection = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
DataTable dt = new DataTable();
OracleCommand command = new OracleCommand(select, connection);
command.CommandTimeout = 900000000;
DataSet dataset = new DataSet();
OracleDataAdapter adapter = new OracleDataAdapter(command);
adapter.Fill(dataset);
dt = dataset.Tables[0];
List<BVNNotEnrolledAllBranches> gridRecords = new List<BVNNotEnrolledAllBranches>();
BVNNotEnrolledAllBranches bvnFeilds = new BVNNotEnrolledAllBranches();
lblNoRecord.Text = "Total record(s): " + dt.Rows.Count.ToString();
lblNoRecord.ForeColor = System.Drawing.Color.Green;
foreach (System.Data.DataRow dr in dt.Rows)
{
//TransactionModel txn = new TransactionModel();
bvnFeilds.CustomerName = (dr["Customer_name"].ToString());
bvnFeilds.AccountNumber = (dr["Account_number"].ToString());
bvnFeilds.CIF = (dr["CIF_ID"].ToString());
bvnFeilds.PhoneNumber = (dr["Phone_Number"].ToString());
bvnFeilds.BranchName = (dr["BRANCH_NAME"].ToString());
bvnFeilds.BVN = (dr["BVN"].ToString());
bvnFeilds.Email = (dr["Email"].ToString());
//bvnFeilds.Status = (dr["Status"].ToString());
gridRecords.Add(bvnFeilds);
}
GridView1.DataSource = gridRecords;
GridView1.DataBind();
}
请问有什么我错过的吗?在此先感谢。
Please is there something i missed out? Thanks in advance.
推荐答案
你快到了。
bvnFeilds(bvnFields?)不是在循环中重新实例化。您有一个单个实例可以在每个循环中重写。我在下面更正了
You're almost there.
bvnFeilds (bvnFields?) is not re-instantiated within the loop. You have one single instance that gets rewritten every loop. I have corrected below
using (OracleConnection connection = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
DataTable dt = new DataTable();
OracleCommand command = new OracleCommand(select, connection);
command.CommandTimeout = 900000000;
DataSet dataset = new DataSet();
OracleDataAdapter adapter = new OracleDataAdapter(command);
adapter.Fill(dataset);
dt = dataset.Tables[0];
List<bvnnotenrolledallbranches> gridRecords = new List<bvnnotenrolledallbranches>();
lblNoRecord.Text = "Total record(s): " + dt.Rows.Count.ToString();
lblNoRecord.ForeColor = System.Drawing.Color.Green;
foreach (System.Data.DataRow dr in dt.Rows)
{
//Make a new one each pass
BVNNotEnrolledAllBranches bvnFeilds = new BVNNotEnrolledAllBranches();
//TransactionModel txn = new TransactionModel();
bvnFeilds.CustomerName = (dr["Customer_name"].ToString());
bvnFeilds.AccountNumber = (dr["Account_number"].ToString());
bvnFeilds.CIF = (dr["CIF_ID"].ToString());
bvnFeilds.PhoneNumber = (dr["Phone_Number"].ToString());
bvnFeilds.BranchName = (dr["BRANCH_NAME"].ToString());
bvnFeilds.BVN = (dr["BVN"].ToString());
bvnFeilds.Email = (dr["Email"].ToString());
//bvnFeilds.Status = (dr["Status"].ToString());
gridRecords.Add(bvnFeilds);
}
GridView1.DataSource = gridRecords;
GridView1.DataBind();
}
</bvnnotenrolledallbranches></bvnnotenrolledallbranches>
在循环中初始化 bvnFeilds 。
Initialize bvnFeilds inside the loop.
foreach (System.Data.DataRow dr in dt.Rows)
{
bvnFeilds = new BVNNotEnrolledAllBranches();
这就是全部!
希望,它有帮助:)
That's all !
Hope, it helps :)
<pre lang="cs">BVNNotEnrolledAllBranches bvnFeilds = new BVNNotEnrolledAllBranches();
< br $>
被脱颖而出ACH。因此,将其放在foreach语句中将解决问题。
was instantiated out side the foreach. So putting this in the foreach statement will solve the problem.
这篇关于如何将List绑定到gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文