public static string OutData = "";     //数据库名
        public static string OutTable = "";    //数据表名
        public string strserver = "";
        public string struser = "";
        public string strpwd = "";
        DataSet dsSet = new DataSet();        //加载窗体时,显示所有数据,每行数据前都有一个checkbox
         private void DataForm_Load(object sender, EventArgs e)
        {
            groupBox1.Text = "数据表名称:" + OutTable; //设置标题
            try
            {
                using (SqlConnection con = new SqlConnection("Server=" + strserver + ";database=" + OutData + ";Uid=" + struser + ";Pwd=" + strpwd))
                {
                    string strSql = "select * from "+ OutTable;
                    con.Open();
                    SqlDataAdapter da = new SqlDataAdapter(strSql, con);
                    da.Fill(dsSet,"daoru");
                    //显示数据表中的数据
                    this.dataGridView1.DataSource = dsSet.Tables["daoru"];
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }        //选中要删除的checkbox后,点击删除按钮
        private void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection con = new SqlConnection("Server=" + strserver + ";database=" + OutData + ";Uid=" + struser + ";Pwd=" + strpwd))
                {
                    string strSql = "";
                    string strid = "";     //用于选中行的id的集合
                    int count = 0;         //选中要删除的行数                    #region sql语句
                    for (int i = dataGridView1.Rows.Count-1; i >=0; i--)
                    {
                        if (dataGridView1.Rows[i].Cells[0].Value == null)
                        {
                            continue;                        }
                        else
                        {
                            if (i == 0)
                            {
                                strid += dataGridView1.Rows[i].Cells[1].Value.ToString();
                            }
                            else
                            {
                                strid += dataGridView1.Rows[i].Cells[1].Value.ToString() + ",";
                            }                            count++;                        }
                    }    
                    strSql = "Delete from " + OutTable + " where " + dataGridView1.Columns[1].HeaderText.ToString() + " in (" + strid + " )"; ;
                    #endregion                    con.Open();
                    SqlDataAdapter da = new SqlDataAdapter(strSql, con);
                    SqlCommandBuilder thisBuilder = new SqlCommandBuilder(da);
                    da.Update(dsSet,"daoru");
                    this.dataGridView1.DataSource = dsSet.Tables["daoru"];
                    con.Close();
                    }
                }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
          }问题是,为什么我点击完删除按钮后,整个视图都没有了数据显示,而且删除也没有实现,本身的sql语句是没有问题的,请高手指教!

解决方案 »

  1.   

    SqlDataAdapter da = new SqlDataAdapter(strSql, con);
                        SqlCommandBuilder thisBuilder = new SqlCommandBuilder(da);
                        da.Update(dsSet,"daoru");
                        this.dataGridView1.DataSource = dsSet.Tables["daoru"];1,你传入的sql 是删除 不会返回数据集吧!
    2,你传入的sql 放在查询分析器里 执行一下!
      

  2.   

    我在数据库中执行sql语句可以实现删除功能,我只是不会如何把删除后的数据信息重新添加到dataGridView中
      

  3.   

    你那个判断语句是不是不太对啊
    你试试 换成 :(bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == false