想对GridView绑定数据源...
在DAL层写的代码:
public static List<user> SelectAll()
{
string _sql = "select * from users";
DataTable dt = DBHelper.GetDataSet(_sql); if (dt == null || dt.Rows.Count == 0)
{
return null;
}
else
{
List<user> list = new List<user>();
foreach ( DataRow row in dt.Rows)
{
list.Add(CreateInstance(row));
}
return list;
}
}
private static user CreateInstance(DataRow dr)
{
user model = new user();
model.User_ID = int.Parse(dr["user_ID"].ToString());
model.User_name = dr["user_name"].ToString();
model.Pwd = dr["pwd"].ToString();
model.Sex = dr["sex"].ToString();
model.Birthday=DateTime.Parse(dr["birthday"].ToString());
model.Phone = dr["phone"].ToString();
model.Addr=dr["addr"].ToString();
model.Email=dr["email"].ToString(); return model;
}
在BLL层: public static List<user> UserBind( ) { return userDAL.SelectAll( );}
在页面的对GridView绑定:
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = userBLL.UserBind();
GridView1.DataBind();
}
不知道哪里出了问题,页面上没有显示出数据但是 数据库中有一条记录,会显示空的一行。有两条记录,就有空的两行被弄晕了、这是怎么一回事呢?请赐教啊!!
在DAL层写的代码:
public static List<user> SelectAll()
{
string _sql = "select * from users";
DataTable dt = DBHelper.GetDataSet(_sql); if (dt == null || dt.Rows.Count == 0)
{
return null;
}
else
{
List<user> list = new List<user>();
foreach ( DataRow row in dt.Rows)
{
list.Add(CreateInstance(row));
}
return list;
}
}
private static user CreateInstance(DataRow dr)
{
user model = new user();
model.User_ID = int.Parse(dr["user_ID"].ToString());
model.User_name = dr["user_name"].ToString();
model.Pwd = dr["pwd"].ToString();
model.Sex = dr["sex"].ToString();
model.Birthday=DateTime.Parse(dr["birthday"].ToString());
model.Phone = dr["phone"].ToString();
model.Addr=dr["addr"].ToString();
model.Email=dr["email"].ToString(); return model;
}
在BLL层: public static List<user> UserBind( ) { return userDAL.SelectAll( );}
在页面的对GridView绑定:
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = userBLL.UserBind();
GridView1.DataBind();
}
不知道哪里出了问题,页面上没有显示出数据但是 数据库中有一条记录,会显示空的一行。有两条记录,就有空的两行被弄晕了、这是怎么一回事呢?请赐教啊!!
解决方案 »
- 关于购物车的代码修改~~小女子请求帮助~~~
- 模态窗口怎么弄的?
- 在asp.net中生成word时遇到的权限问题
- 怎样根据数据库内容改网页title?
- 如何确认删除呢??
- 返回当前页面的名称
- 求解,如何将指定的HTML代码插入 asp.net / asp / php / htm 页面
- 初学者提问,我在C:\Inetpub\wwwroot 下放了个defaut.aspx怎样才能将他设置成默认主页?
- 哪位前辈能指点一下我啊。怎么分页?
- 请各位推荐一下国内比较好的英文原版电子书下载地址
- Repeater下面操作出问题,急!急!急!(分虽不高,各位前辈帮帮忙了)
- 关于ASP.net实现Jmail.......
{
GridView1.DataSource = userBLL.UserBind();
GridView1.DataBind();
}
你的GridView1在前台进行字段绑定了吗?
或者你设置autocoloums为true了没?
没绑定怎么显示?