DATAGRIDVIEW动态筛选数据,实现编辑之后,点击按钮更新,及删除选中的列到数据库。

来源:互联网 发布:淘宝c店没有流量咋办 编辑:程序博客网 时间:2024/05/16 04:57

我是建一个专门连接数据库和一些操作数据库的的类,不说话,直接上代码。

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace SMS.BaseClass
{
    class DataCon
    {
        public SqlConnection getcon()
        {
            string sqlstr = "Data Source=10.16.100.98;Database=ynkc;uid = SA; pwd=990904";
           //string sqlstr = "Data Source=ZSCHSERVER1-IBM\\SQLEXPRESS;Database=chgltest;uid = SA; pwd=990904";
            //string sqlstr = "Data Source=10.16.100.98;Database=ynkc;uid = SA; pwd=990904";
            SqlConnection Mycon = new SqlConnection(sqlstr);
            //Mycon.Open();
            return Mycon;

       
        }


        public void getcom(string comstr)
        {
            SqlConnection sqlcon = this.getcon();
            sqlcon.Open();
            SqlCommand sqlcom = new SqlCommand(comstr, sqlcon);
            sqlcom.ExecuteNonQuery();
            sqlcom.Dispose();
            sqlcon.Close();
            sqlcon.Dispose();

       
        }


     

 

        public DataSet getds(string sqlstr,string tbstr)
        {
            SqlConnection sqlcon = this.getcon();
            DataSet myds = new DataSet();

            SqlDataAdapter myad = new SqlDataAdapter(sqlstr, sqlcon);
            myad.Fill(myds, tbstr);
            return myds;
       
        }

        public SqlDataReader getread(string sqlstr)
        {
            SqlConnection sqlcon = this.getcon();
            SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
            sqlcon.Open();
            SqlDataReader sqlread = sqlcom.ExecuteReader(CommandBehavior.CloseConnection);

           

            return sqlread;
       
        }
     

    }
}

 

 

然后就是在From里面设置好界面,写代码

 

  private void btnok_Click(object sender, EventArgs e)
        {
            ds.Clear();

            if (txtss.Text == "")
            {
                ds = datacon.getds("select id as 编号,werks as 工厂 from ynkc", "ynkc");
                gvdata.DataSource = ds.Tables[0];
                frmISManage_Load(sender, e);
                //MessageBox.Show("请输入关键字!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {
                string sqlCheckStr = "select id as 编号,werks as 工厂 ,maktx as 代码 from ynkc where maktx like '%" + txtss.Text + "%'";
                SqlConnection cn = new SqlConnection();
                da1 = new SqlDataAdapter(sqlCheckStr, datacon.getcon());
                da1.Fill(ds, "Table");
                gvdata.DataSource = ds.Tables["Table"];
               // cn.Dispose();
               // da1.Dispose();


                //DataSet myds = datacon.getds("select id as 编号,werks as 工厂 from ynkc where maktx like '%" + txtss.Text + "%'", "ynkc");
                //gvdata.DataSource = myds.Tables[0];


                frmISManage_Load(sender, e);

 

            }

        }

     

        private void button1_Click(object sender, EventArgs e)
        {
           try

            {
              

                SqlCommandBuilder com = new SqlCommandBuilder(da1);

                da1.Update(ds.Tables["Table"]);
                ds.Clear();
                da1.Fill(ds, "Table");

                MessageBox.Show("更新完成");

            }

            catch (Exception)

            {

                MessageBox.Show("Fail");

            }

 

        }

 

 private void btndel_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.gvdata.Rows.Count <= 0)
                { MessageBox.Show("没有要删除的内容"); }
                else
                {
                    DialogResult result = MessageBox.Show("确认删除此条记录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (result == DialogResult.Yes)
                    {
                        string delete_No = this.gvdata.SelectedRows[0].Cells["编号"].Value.ToString();
                        string sql_str_delete = "delete from ynkc where id='" + delete_No + "'";
                        SqlConnection conn = datacon.getcon();
                        SqlCommand com = new SqlCommand(sql_str_delete, conn);
                        datacon.getcom(sql_str_delete);

                        this.gvdata.Rows.RemoveAt(this.gvdata.SelectedRows[0].Index);
                        MessageBox.Show("记录已成功删除", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

 

原创粉丝点击