代码实现GirdView控件显示数据库数据出错,请大家帮忙看看
提示错误:数据源的类型无效。它必须是 IListSource、IEnumerable 或 IDataSource。
后台文件:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
public void bind()
{
string sql = string.Format("select * from UserInfo");
MySqlConnection conn = new MySqlConnection("server=192.168.0.254;user id=root;password=110120110;database=dlxj;charset=gb2312");
conn.Open();
MySqlDataAdapter myda = new MySqlDataAdapter(sql, conn);
DataSet myds = new DataSet();
myda.Fill(myds);
GridView1.DataSource = myda;
//GridView1.DataKeyNames = new string[] { "Id" };
GridView1.DataBind();
conn.Close();
}
}
页面文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
新手练习,请大家帮帮忙
直接配置数据源已经可以正常显示了,就是代码实现出了问题
[解决办法]
GridView1.DataSource = myds;