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(); } } }
上面没发好,主要是这里了 也许你已经搞定了 //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(); }
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但是数据库中的记录还在。
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();
}
}
}
也许你已经搞定了
//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();
}