我想在我的 Asp.new c# 表单的网格视图中显示我的数据。我正在使用 Wamp Server 的 MySQL 数据库。我已经厌倦了将数据库与网格视图绑定。请帮忙。
我的代码:
public partial class Temp : System.Web.UI.Page
{
string conString = ConfigurationManager.ConnectionStrings["AASProject"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection(conString);
con.Open();
gvFoodDetail.RowEditing+= new GridViewEditEventHandler(gvFoodDetail_RowEditing);
gvFoodDetail.RowDeleting += new GridViewDeleteEventHandler(gvFoodDetail_RowDeleting);
gvFoodDetail.RowUpdating += new GridViewUpdateEventHandler(gvFoodDetail_RowUpdating);
//gvFoodDetail_RowCancelingEdit += new GridViewCancelEditEventArgs(gv);
// gvFoodDetail.RowEditing += new GridViewEditEventHandler(gvFoodDetail_RowEditing);
// gvFoodDetail.RowDeleting += new GridViewDeleteEventHandler(gvFoodDetail_RowDeleting);
// gvFoodDetail.RowUpdating += new GridViewUpdateEventHandler(gvFoodDetail_RowUpdating);
// gvFoodDetail.RowCancelingEdit += new GridViewCancelEditEventHandler(gvFoodDetail_RowCancelingEdit);
gvFoodDetail.PageIndexChanging += new GridViewPageEventHandler(gvFoodDetail_PageIndexChanging);
if (!IsPostBack)
{
gvFoodDetail.Visible = true;
loadFoodDB();
}
}
public void loadFoodDB()
{
string getFoodDetails = "Select Emp_ID,intime,outtime,date From datetime";
MySqlConnection con = new MySqlConnection(conString);
con.Open();
MySqlCommand cmd = new MySqlCommand(getFoodDetails, con);
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
gvFoodDetail.DataSource = dt;
gvFoodDetail.DataBind();
}