想通过dataGrid控件对数据库实现 添加 删除 修改 操作请懂的朋友给予帮助 谢谢

解决方案 »

  1.   

    这种问题网上很好找的,例如这篇:
    http://www.chenjiliang.com/Article/View.aspx?ArticleID=1162&TypeID=5
      

  2.   

    //数据绑定
    private void BindGrid()
        {
            SqlConnection con = new SqlConnection(ConnectionString);
            con.Open();
            string sql = "select [co_id]as[会员号],[co_name]as[公司名],[co_shortname]as[公司简称]
            from jq_fitment";
            SqlCommand com = new SqlCommand(sql, con);
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = com;
            DataSet ds = new DataSet();
            da.Fill(ds, "jinqi");
            con.Close();
            GridView1.DataSource = ds.Tables[0].DefaultView;
            GridView1.DataBind();
        }
    //数据更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
          //  GridView1.FooterRow.Enabled = false;
            string co_id = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString());
            string co_name = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString());
            string co_shortname = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString());
            string co_passwd = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString());
            string co_location = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString());
            string co_product_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString());
            string co_product_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.ToString());
            string co_product_3 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text.ToString());
            string co_product_other = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text.ToString());
            string co_deputy = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text.ToString());
            string co_tel = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[11].Controls[0]).Text.ToString());
            string co_tel_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[12].Controls[0]).Text.ToString());
            string co_tel_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[13].Controls[0]).Text.ToString());
            string co_fax = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[14].Controls[0]).Text.ToString());
            string co_email_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[15].Controls[0]).Text.ToString());
            string co_email_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[16].Controls[0]).Text.ToString());
            string co_web = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[17].Controls[0]).Text.ToString());
            string co_adress = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[18].Controls[0]).Text.ToString());
            string co_info = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[19].Controls[0]).Text.ToString());        try
            {
                SqlConnection con = new SqlConnection(ConnectionString);
                con.Open();
                string update = "update jq_fitment set co_name='" + co_name + "',co_shortname='" + co_shortname + "',co_passwd='" + co_passwd + "',co_location='" + co_location + "',co_product_1='" + co_product_1 + "',co_product_2='" + co_product_2 + "',co_product_3='" + co_product_3 + "',co_product_other='" + co_product_other + "',co_deputy='" + co_deputy + "',co_tel='" + co_tel + "',co_tel_1='" + co_tel_1 + "',co_tel_2='" + co_tel_2 + "',co_fax='" + co_fax + "',co_email_1='" + co_email_1 + "',co_email_2='" + co_email_2 + "',co_web='" + co_web + "',co_adress='" + co_adress + "',co_info='" + co_info + "'where co_id='" + co_id + "'";
                SqlCommand com = new SqlCommand(update, con);
                int a = com.ExecuteNonQuery();
                GridView1.EditIndex = -1;
                BindGrid();
            }
            catch (Exception ex)
            {
                Response.Write("<script> alert ('" + ex.Message + "') </script>");
            }    }
    //取消
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            this.BindGrid();
        }
    //删除
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                string a = this.GridView1.Rows[e.RowIndex].Cells[1].Text.ToString();
                SqlConnection con = new SqlConnection(ConnectionString);
                con.Open();
                string sql = "delete from jq_fitment where co_id='" + a + "'";
                SqlCommand com = new SqlCommand(sql, con);
                com.ExecuteNonQuery();
                this.BindGrid();
            }
            catch (Exception ex)
            {
                Response.Write("<script> alert('" + ex.Message + "') </script>");
            }
        }
    //编辑
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
      

  3.   

    //数据绑定
    private void BindGrid()
        {
            SqlConnection con = new SqlConnection(ConnectionString);
            con.Open();
            string sql = "select [co_id]as[会员号],[co_name]as[公司名],[co_shortname]as[公司简称]
            from jq_fitment";
            SqlCommand com = new SqlCommand(sql, con);
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = com;
            DataSet ds = new DataSet();
            da.Fill(ds, "jinqi");
            con.Close();
            GridView1.DataSource = ds.Tables[0].DefaultView;
            GridView1.DataBind();
        }
    //数据更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
          //  GridView1.FooterRow.Enabled = false;
            string co_id = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString());
            string co_name = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString());
            string co_shortname = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString());
            string co_passwd = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString());
            string co_location = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString());
            string co_product_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString());
            string co_product_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.ToString());
            string co_product_3 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text.ToString());
            string co_product_other = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text.ToString());
            string co_deputy = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text.ToString());
            string co_tel = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[11].Controls[0]).Text.ToString());
            string co_tel_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[12].Controls[0]).Text.ToString());
            string co_tel_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[13].Controls[0]).Text.ToString());
            string co_fax = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[14].Controls[0]).Text.ToString());
            string co_email_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[15].Controls[0]).Text.ToString());
            string co_email_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[16].Controls[0]).Text.ToString());
            string co_web = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[17].Controls[0]).Text.ToString());
            string co_adress = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[18].Controls[0]).Text.ToString());
            string co_info = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[19].Controls[0]).Text.ToString());        try
            {
                SqlConnection con = new SqlConnection(ConnectionString);
                con.Open();
                string update = "update jq_fitment set co_name='" + co_name + "',co_shortname='" + co_shortname + "',co_passwd='" + co_passwd + "',co_location='" + co_location + "',co_product_1='" + co_product_1 + "',co_product_2='" + co_product_2 + "',co_product_3='" + co_product_3 + "',co_product_other='" + co_product_other + "',co_deputy='" + co_deputy + "',co_tel='" + co_tel + "',co_tel_1='" + co_tel_1 + "',co_tel_2='" + co_tel_2 + "',co_fax='" + co_fax + "',co_email_1='" + co_email_1 + "',co_email_2='" + co_email_2 + "',co_web='" + co_web + "',co_adress='" + co_adress + "',co_info='" + co_info + "'where co_id='" + co_id + "'";
                SqlCommand com = new SqlCommand(update, con);
                int a = com.ExecuteNonQuery();
                GridView1.EditIndex = -1;
                BindGrid();
            }
            catch (Exception ex)
            {
                Response.Write("<script> alert ('" + ex.Message + "') </script>");
            }    }
    //取消
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            this.BindGrid();
        }
    //删除
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                string a = this.GridView1.Rows[e.RowIndex].Cells[1].Text.ToString();
                SqlConnection con = new SqlConnection(ConnectionString);
                con.Open();
                string sql = "delete from jq_fitment where co_id='" + a + "'";
                SqlCommand com = new SqlCommand(sql, con);
                com.ExecuteNonQuery();
                this.BindGrid();
            }
            catch (Exception ex)
            {
                Response.Write("<script> alert('" + ex.Message + "') </script>");
            }
        }
    //编辑
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGrid();
        }