DataGridView全部删除不成功?

bulls5988 2010-11-02 10:19:30
现在button3的dataGridView1.Rows.Clear();删除全部记录,现在
不成功,提示 "不能清除此列表。" 请教是什么原因?

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 cj_sys
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
string conn_str = "Server=WIN_TEST;Database=choujiang;uid=sa;pwd=dsd_dsdn";
string sql_str = "select cj_names as 品牌名称,cj_show_counts as 可抽奖数,cj_old_counts as 原抽奖数,cj_pic_path as 商标路径,cj_date as 加入时间 from cj_main where cj_flag=0 order by cj_date desc";
private SqlDataAdapter find_do;
private DataSet ds;

private void Form4_Load(object sender, EventArgs e)
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
SqlConnection conn = new SqlConnection(conn_str);
SqlCommand cmd = new SqlCommand(sql_str,conn);
find_do = new SqlDataAdapter();

find_do.SelectCommand = cmd;

ds = new DataSet();
find_do.Fill(ds,"rs");
dataGridView1.DataSource =ds.Tables["rs"];
dataGridView1.Columns[0].FillWeight = 50;
dataGridView1.Columns[1].FillWeight = 50;
dataGridView1.Columns[2].FillWeight = 50;
dataGridView1.Columns[3].FillWeight = 260;
dataGridView1.Columns[4].FillWeight = 70;
dataGridView1.Columns[2].ReadOnly = true;
dataGridView1.Columns[4].ReadOnly = true;
}

private void button1_Click(object sender, EventArgs e)
{
SqlCommandBuilder updates = new SqlCommandBuilder(find_do);
find_do.UpdateCommand = updates.GetUpdateCommand();
find_do.Update(ds,"rs");
dataGridView1.Update();
}

private void button2_Click(object sender, EventArgs e)
{
SqlCommandBuilder delete = new SqlCommandBuilder(find_do);
find_do.DeleteCommand = delete.GetDeleteCommand();
dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
find_do.Update(ds, "rs");
dataGridView1.Update();
}

private void button3_Click(object sender, EventArgs e)
{
SqlCommandBuilder delete = new SqlCommandBuilder(find_do);
find_do.DeleteCommand = delete.GetDeleteCommand();
dataGridView1.Rows.Clear();
find_do.Update(ds, "rs");
dataGridView1.Update();
}


}
}

...全文
375 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
josxhn 2010-11-09
  • 打赏
  • 举报
回复
上面没发好,主要是这里了
也许你已经搞定了


//delete data in [test_2] while remove each rows in dataGridView
private void btn_ClearTable_Click(object sender , EventArgs e)
{
conn.Open();
cb.DataAdapter = da;
cb.GetDeleteCommand();

if (dataGridView1.Rows.Count > 0)
foreach (DataGridViewRow dr in dataGridView1.Rows)
//look at here please
dataGridView1.Rows.Remove(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex]);

da.Update(ds.Tables[0]);
conn.Close();
}
josxhn 2010-11-09
  • 打赏
  • 举报
回复

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

namespace test_DeleteTableThroughDataGridView
{
public partial class Form1 : Form
{
public SqlDataAdapter da;
public SqlConnection conn;
public SqlCommand cmd;
public SqlCommandBuilder cb;
public DataSet ds;

public string CmdText_Select = "select * from [test_2]";

public Form1()
{
InitializeComponent();
conn = new SqlConnection("Data Source=LX-WORK;Initial Catalog=testdb;Integrated Security=True");
cmd = new SqlCommand();
cb = new SqlCommandBuilder();
da = new SqlDataAdapter();
ds = new DataSet();
}

private void Form1_Load(object sender , EventArgs e)
{
}

//delete data in [test_2] while remove each rows in dataGridView
private void btn_ClearTable_Click(object sender , EventArgs e)
{
conn.Open();
cb.DataAdapter = da;
cb.GetDeleteCommand();

if (dataGridView1.Rows.Count > 0)
foreach (DataGridViewRow dr in dataGridView1.Rows)
//look at here please dataGridView1.Rows.Remove(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex]);

da.Update(ds.Tables[0]);
conn.Close();
}

//show data
private void btn_Show_Click(object sender , EventArgs e)
{
dataGridView1.DataSource = null;
conn.Open();
cmd.CommandText = CmdText_Select;
cmd.Connection = conn;
da.SelectCommand = cmd;
da.Fill(ds , "tb1");
dataGridView1.DataSource = ds.Tables[0];
conn.Close();
}

private void btn_ESC_Click(object sender , EventArgs e)
{
this.Close();
this.Dispose();
}
}
}

bulls5988 2010-11-03
  • 打赏
  • 举报
回复
当然是要清空数据库这个表中所有的数据了
josxhn 2010-11-03
  • 打赏
  • 举报
回复
你是清空DGV里的内容还是要删除对应数据库的数据
bulls5988 2010-11-03
  • 打赏
  • 举报
回复
没人知道么?
bulls5988 2010-11-02
  • 打赏
  • 举报
回复

SqlCommandBuilder delete = new SqlCommandBuilder(find_do);
find_do.DeleteCommand = delete.GetDeleteCommand();
dataGridView.Datasource=null;
dataGridView1.Rows.Clear();
find_do.Update(ds, "rs");
dataGridView1.Update();


这样虽然清除了,页面上的DataGridView但是数据库中的记录还在。
bulls5988 2010-11-02
  • 打赏
  • 举报
回复
用foreach遍历删除所有行?
YangYun 2010-11-02
  • 打赏
  • 举报
回复
我也遇到过这个问题,用clear()方法不能清除掉,我最后的解决办法是将与之关联的Datasource,比如DataTable中删除所有datarow后就可以了。

我认为是不是这样的,如果datagridview有了绑定的datasource后,使用clear()就不行,如果直接在items里进行赋值的话,这个时候clear()就可以了,具体是不是这样,还没有进行验证。

111,089

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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