[英]How to display list<> in gridview ASP.NET C#
I have DAL code in asp.net c# for select all user of my table_users in DB sql server. 我在asp.net c#中有DAL代码,用于选择DB sql服务器中table_users的所有用户。
my DAL : 我的DAL:
public class Users
{
private string _UserName;
public string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
private string _Pass;
public string Pass
{
get { return _Pass; }
set { _Pass = value; }
}
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
private string _Family;
public string Family
{
get { return _Family; }
set { _Family = value; }
}
private string _Mobile;
public string Mobile
{
get { return _Mobile; }
set { _Mobile = value; }
}
}
public List<Users> GetAllUsers()
{
List<Users> usersList = null;
using (DataTable table = SqlDBHelper.ExecuteSelectCommand("GetAllUsers", CommandType.StoredProcedure))
{
if (table.Rows.Count > 0)
{
usersList = new List<Users>();
foreach (DataRow row in table.Rows)
{
Users user = new Users();
user.UserName = table.Rows[0]["UserName"].ToString();
user.Pass = table.Rows[0]["Pass"].ToString();
user.Name = table.Rows[0]["Name"].ToString();
user.Family = table.Rows[0]["Family"].ToString();
user.Mobile = table.Rows[0]["Mobile"].ToString();
usersList.Add(user);
}
}
}
return usersList;
}
and my BLL code for call: 和我的BLL通话代码:
public List<Users> GetAllUsers()
{
return userDB.GetAllUsers();
}
and now I want to call method for display data in grid view . 现在我想调用在网格视图中显示数据的方法。
public List<Users> usersList = null;
UsersHadler userDB = new UsersHadler();
usersList = userDB.GetAllUsers();
foreach (Users user1 in usersList)
{
GridView1.DataSource = usersList;
GridView1.DataBind();
}
I have 3 user in data base.uer1 and user2 and user3 the problem is that I see in gridview 3 row for one user1. 我在base.uer1和user2和user3中有3个用户, 问题是我在gridview 3行中看到一个user1。 user1 user1 user1 what is wrong.? user1 user1 user1怎么了? thanks for reply 谢谢您的回复
` `
Get rid of the foreach
loop. 摆脱foreach
循环。 You are going through usersList
once for each user and binding the entire list to GridView1
. 您将为每个用户usersList
一次usersList
并将整个列表绑定到GridView1
。 You only need to do it once. 您只需要做一次。
public List<Users> usersList = null;
UsersHadler userDB = new UsersHadler();
usersList = userDB.GetAllUsers();
GridView1.DataSource = usersList;
GridView1.DataBind();
You also probably want to call the GetAllUsers
method from your BLL instead of calling the one from the DAL directly in your view. 您可能还想从BLL中调用GetAllUsers
方法,而不是直接在视图中从DAL中调用该方法。 Otherwise, what was the point of the BLL? 否则,BLL的意义何在?
Your problem is in the DAL version of GetAllUsers
. 您的问题出在GetAllUsers
的DAL版本中。 You are referring only to Rows[0]
in the foreach
loop. 您仅在foreach
循环中引用Rows[0]
。
user.UserName = table.Rows[0]["UserName"].ToString();
user.Pass = table.Rows[0]["Pass"].ToString();
user.Name = table.Rows[0]["Name"].ToString();
user.Family = table.Rows[0]["Family"].ToString();
user.Mobile = table.Rows[0]["Mobile"].ToString();
Replace the foreach
loop in the DAL copy of GetAllUsers
with this: 用以下代码替换GetAllUsers
的DAL副本中的foreach
循环:
foreach (DataRow row in table.Rows)
{
Users user = new Users();
user.UserName = row["UserName"].ToString();
user.Pass = row["Pass"].ToString();
user.Name = row["Name"].ToString();
user.Family = row["Family"].ToString();
user.Mobile = row["Mobile"].ToString();
usersList.Add(user);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.