当前位置:编程学习 > C#/ASP.NET >>

在运行中的 DataGridView中添加一个删除行 然后执行删除

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 删除作业
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataGridViewCheckBoxColumn dgcvbc = new DataGridViewCheckBoxColumn();
            SqlConnection conn = new SqlConnection("server=.;database=test;uid=sa;pwd=sa");
            SqlCommand cmd = new SqlCommand("select * from sc", conn);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);

            DataGridViewColumn aa = new DataGridViewColumn();
            DataGridViewButtonColumn aaa = new DataGridViewButtonColumn();
            aaa.Text = "删除";
            aaa.UseColumnTextForButtonValue = true;
            dataGridView1.DataSource = dt;
            dataGridView1.Columns.Add(aaa);
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "删除")
            {

                SqlConnection conn = new SqlConnection("server=.;database=test;uid=sa;pwd=sa");
                SqlCommand cmd = new SqlCommand("select * from sc ,student where sc.intsid=student.intsid", conn);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable dtsc = new DataTable();
                sda.Fill(dtsc);
                DataGridViewSelectedRowCollection dgv = dataGridView1.SelectedRows;
                for (int i = 0; i < dgv.Count; i++)
                {
                    string strDe2 = "delete sc where intsid='" + dtsc.Rows[dgv[i].Index]["intsid"] + "'";
                    string strDe1 = "delete student where intsid='" + dtsc.Rows[dgv[i].Index]["intsid"] + "'";
                    SqlHepler sh1 = new SqlHepler();
                    sh1.DedTable(strDe1);
                    sh1.DedTable(strDe2);
                }
                SqlHepler sh2 = new SqlHepler();
                sh2.GetTable("select * from sc join student on sc.intsid=student.intsid");
                dtsc = new DataTable();
                dataGridView1.DataSource = dtsc;
            }
        }
    }
}


在数据库中的Test中,有sc和student两个表 ;   
在运行中的 DataGridView中添加一个删除行 然后点击删除   但是不能执行    求解释....... --------------------编程问答-------------------- 抓一下断点。 --------------------编程问答-------------------- 代码好乱啊 --------------------编程问答-------------------- 看起来比较费劲
补充:.NET技术 ,  其他语言
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,