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

CC码 2011-11-07 10:16:49
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中添加一个删除行 然后点击删除 但是不能执行 求解释.......
...全文
130 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tyg111 2012-03-01
  • 打赏
  • 举报
回复
看起来比较费劲
夜色镇歌 2011-12-28
  • 打赏
  • 举报
回复
代码好乱啊
shaoyank 2011-11-10
  • 打赏
  • 举报
回复
抓一下断点。

1,979

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧