C# DataGridView使用SqlDataAdapter.Update删除和修改数据库

首先在DataGridView添加选择框(类型为DataGridViewCheckBoxColumn)。确保数据表设置主键。

//数据库操作变量
DataSet dstSql = new DataSet();
SqlDataAdapter dadstrSql= new SqlDataAdapter();
//数据库加载函数
private void DisplayData ()
{
    string strSql = "select * from info order by time desc";
    dadstrSql= new SqlDataAdapter(strSql, conConnect);
    dstSql.Clear();
    dadstrSql.Fill(dstSql, "info ");
    Dgv.DataSource = dstSql.Tables["info "];
 }
//窗体加载
private void frmData_Load(object sender, EventArgs e)
{
DisplayData ();//数据库加载
}

//删除事件
        private void btnDelete_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (DataGridViewRow row in Dgv.Rows)
            {
                if (row.Cells["选择"].EditedFormattedValue.ToString() == "True")
                {
                    i++;
                }
            }
            if (i > 0)
            {
                if (MessageBox.Show("确定删除吗?", "删除提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    List rows = new List();
                    foreach (DataGridViewRow row in Dgv.Rows)
                    {
                        if (row.Cells["选择"].EditedFormattedValue.ToString() == "True")
                        {
                            rows.Add(row);
                        }

                    }
                    foreach (DataGridViewRow row in rows)
                    {
                        Dgv.Rows.Remove(row);
                        SqlCommandBuilder SCB = new SqlCommandBuilder(dadstrSql);
                        dadstrSql.Update(dstSql, "info ");

                    }
                    MessageBox.Show("删除成功!" + "共删除" + i + "行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("请选中要删除的行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
//修改事件

        private void btnAlter_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (DataGridViewRow row in Dgv.Rows)
            {
                if (row.Cells["选择"].EditedFormattedValue.ToString() == "True")
                {
                    i++;
                }

            }
            if (i > 0)
            {
                if (MessageBox.Show("确定修改吗?", "修改提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    List rows = new List();
                    foreach (DataGridViewRow row in Dgv.Rows)
                    {
                        if (row.Cells["选择"].EditedFormattedValue.ToString() == "True")
                        {
                            rows.Add(row);
                        }

                    }
                    foreach (DataGridViewRow row in rows)
                    {
                        SqlCommandBuilder SCB = new SqlCommandBuilder(dadstrSql);
                        dadstrSql.Update(dstSql, "info ");
                       
                    }
                    MessageBox.Show("修改成功!" + "共修改" + i + "行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    DisplayData ();//数据库刷新
                }
            }
            else
            {
                MessageBox.Show("请选中要修改的行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

你可能感兴趣的:(C#程序设计)