各位我是个C#菜鸟,碰到个关于datagridview删除行的问题,我做个删除按钮,想按这个按钮就删除指定行的数据,并删除数据库里的这条数据
代码如下:
public partial class Form2 : Form
{
private BindingSource bindingSource1 = new BindingSource();
private OleDbDataAdapter dataAdapter = new OleDbDataAdapter(); public Form2()
{
InitializeComponent();
}
private void GetData(string selectCommand)
{
try
{
// Specify a connection string. Replace the given value with a
// valid connection string for a Northwind SQL Server sample
// database accessible to your system.
String connectionString =
@"provider=microsoft.jet.oledb.4.0;data source=C:\Documents and Settings\wu\My Documents\Visual Studio 2005\Projects\BegVCSharp\Nwind.mdb";
// Create a new data adapter based on the specified query.
dataAdapter = new OleDbDataAdapter (selectCommand, connectionString); // Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dataAdapter); // Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table; // Resize the DataGridView columns to fit the newly loaded content.
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
catch (OleDbException)
{
MessageBox.Show("To run this example, replace the value of the " +
"connectionString variable with a connection string that is " +
"valid for your system.");
}
} private void button2_Click(object sender, EventArgs e)
{
GetData(dataAdapter.SelectCommand.CommandText);
} private void Form2_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = bindingSource1;
GetData("select * from Customers");
}
//删除按钮的事件
private void button1_Click(object sender, EventArgs e)
{
//效果是删除了DataGridView一行,但是没对数据库起作用 dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
}
}请各位大大指点,谢谢
代码如下:
public partial class Form2 : Form
{
private BindingSource bindingSource1 = new BindingSource();
private OleDbDataAdapter dataAdapter = new OleDbDataAdapter(); public Form2()
{
InitializeComponent();
}
private void GetData(string selectCommand)
{
try
{
// Specify a connection string. Replace the given value with a
// valid connection string for a Northwind SQL Server sample
// database accessible to your system.
String connectionString =
@"provider=microsoft.jet.oledb.4.0;data source=C:\Documents and Settings\wu\My Documents\Visual Studio 2005\Projects\BegVCSharp\Nwind.mdb";
// Create a new data adapter based on the specified query.
dataAdapter = new OleDbDataAdapter (selectCommand, connectionString); // Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dataAdapter); // Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table; // Resize the DataGridView columns to fit the newly loaded content.
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
catch (OleDbException)
{
MessageBox.Show("To run this example, replace the value of the " +
"connectionString variable with a connection string that is " +
"valid for your system.");
}
} private void button2_Click(object sender, EventArgs e)
{
GetData(dataAdapter.SelectCommand.CommandText);
} private void Form2_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = bindingSource1;
GetData("select * from Customers");
}
//删除按钮的事件
private void button1_Click(object sender, EventArgs e)
{
//效果是删除了DataGridView一行,但是没对数据库起作用 dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
}
}请各位大大指点,谢谢
解决方案 »
- C# 如何子线程向父线程传送DataSet
- C#调用C语言编写的dll,如何释放内存?
- 我现在在学C#,看到泛型这知识点的时候不是很明白,请教各位
- dataGridView如何删除指定的行?(C# WinForm)
- 得不到存储过程的返回值,求救
- 如何比较两个List的同名属性的值?
- 谁知道北京哪有MCSD.NET(面向C#)的培训?
- propertyGrid很好用,但是在复杂的情况下要想用好它还真的有点难度,所以我想能不能自己做个类似的东西自己用呢?散分
- 能否用此奇葩方法实现网站和客户端的交互
- 求助!请问知乎app这个功能是如何实现的呢?
- 谁帮我看看ASP.net中执行一条带参数存储过程代码错在哪里?
- folderBrowserDialog不能显示目录树
delete from ** where ……
string commandText="delete from table where ID="+"**";
SqlCommand cmd = Connection.CreateCommand();
cmd.CommandText = commandText;
cmd.ExecuteNonQuery();
--------------
这样只是删除的view,即内存数据,这样的修改 没有更新到数据库用的是dataAdapter 就用update更新下数据库就可以了
--------------
这样只是删除的view,即内存数据,这样的修改 没有更新到数据库用的是dataAdapter 就用update更新下数据库就可以了---------------------------------------------------
请高人能写得详细些吗,是用dataAdapter.Update()?那么怎么写()中的呢?
你既然可以得到要删除的行,就可以从行里面取出能为一标示这一行的唯一值,比如说ID。那么删除的话就可以用ADO。NET构建connection,command然后从数据库删除了。
代码;
OleDbConnection thisConn = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0;data source=C:\Documents and Settings\wu\My Documents\Visual Studio 2005\Projects\BegVCSharp\Nwind.mdb");
thisConn.Open();
string commandText = "delete from Customers where CustomerID='" + this.dataGridView1.CurrentRow.Cells[1] + "'";
OleDbCommand thisComm = thisConn.CreateCommand();
thisComm.CommandText = commandText;
thisComm.ExecuteNonQuery();
可能是sql语句的问题
怎么是elete from Customers where CustomerID='DataGridViewTextBoxCell{ColumnIndex=0,RowIndex=0}'
我完全没辙了
不好意思
在删除按钮里写
OleDbConnection thisConn = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0;data source=C:\Documents and Settings\wu\My Documents\Visual Studio 2005\Projects\BegVCSharp\Nwind.mdb");
thisConn.Open();
string commandText = "delete from Customers where CustomerID='" + this.dataGridView1.CurrentRow.Cells[1].Value + "'";//value本来没加
OleDbCommand thisComm = thisConn.CreateCommand();
thisComm.CommandText = commandText;
thisComm.ExecuteNonQuery();