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语句是没有问题的,请高手指教!
解决方案 »
- WinForm中怎样做一个类似于QQ设置中的网络设置下的HTTP代理
- 大并发量该如何处理?
- C#读取mysql中的longtext出现了system.byte[].如何解决啊!!!!
- c# 调用c dll 时出现尝试读取或写入受保护的内存,这通常指示其他内存已损坏
- dataset 的大内高手 请用 犀利的眼光 帮俺 查&修两个 错误!
- 在线急侍............,关于DataSet更新后再写回数据库~~~
- 一简单的文本文件读取问题
- 求教C#自定义数组的问题
- 借用这里的人气,install shield的问题,高手快来
- 关于上传文件的问题
- 图文混排的内容,需要存入数据库再取出来,最后写word,怎么做
- 风暴前夕---我对IPAD的一点看法
SqlCommandBuilder thisBuilder = new SqlCommandBuilder(da);
da.Update(dsSet,"daoru");
this.dataGridView1.DataSource = dsSet.Tables["daoru"];1,你传入的sql 是删除 不会返回数据集吧!
2,你传入的sql 放在查询分析器里 执行一下!
你试试 换成 :(bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == false