简体   繁体   中英

Grid View in Asp.net C# with wamp Server's MYSQL

I want to show my data in grid view in my Asp.new c# form. I am using Wamp Server's MySQL database. I have tired a lot for binding database with grid view. Please do help.

my code:

      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();
    }
datagridview.Datasource = datasource
datagridview.databind()

This should set and show the data. You have to handle the columns and everything manually or automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM