本帖最后由 bulls5988 于 2010-11-02 22:28:57 编辑

解决方案 »

  1.   

    我也遇到过这个问题,用clear()方法不能清除掉,我最后的解决办法是将与之关联的Datasource,比如DataTable中删除所有datarow后就可以了。    我认为是不是这样的,如果datagridview有了绑定的datasource后,使用clear()就不行,如果直接在items里进行赋值的话,这个时候clear()就可以了,具体是不是这样,还没有进行验证。
      

  2.   


    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但是数据库中的记录还在。
      

  3.   

    你是清空DGV里的内容还是要删除对应数据库的数据
      

  4.   


    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();
    }
    }
    }
      

  5.   

    上面没发好,主要是这里了
    也许你已经搞定了
    //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();
    }