各位我是个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);
      
        }      
    }请各位大大指点,谢谢

解决方案 »

  1.   

    你没写删除数据库的代码,怎么可能数据库起作用   呢。 
    delete from ** where ……
      

  2.   

    获得选定行的主键字段
    string commandText="delete from table where ID="+"**";
    SqlCommand cmd = Connection.CreateCommand();
    cmd.CommandText = commandText;
    cmd.ExecuteNonQuery();
      

  3.   

    请高人指点,我这段代码主要是从msdn上看来的,msdn就是没讲到删除,请帮帮忙
      

  4.   

    dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
    --------------
    这样只是删除的view,即内存数据,这样的修改  没有更新到数据库用的是dataAdapter  就用update更新下数据库就可以了
      

  5.   

    this.dataGridView1.CurrentRow.Cells[字段名称]
      

  6.   

    dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
    --------------
    这样只是删除的view,即内存数据,这样的修改 没有更新到数据库用的是dataAdapter 就用update更新下数据库就可以了---------------------------------------------------
    请高人能写得详细些吗,是用dataAdapter.Update()?那么怎么写()中的呢?
      

  7.   

    没用过gridview,用过grid,觉得原理相通。
    你既然可以得到要删除的行,就可以从行里面取出能为一标示这一行的唯一值,比如说ID。那么删除的话就可以用ADO。NET构建connection,command然后从数据库删除了。
      

  8.   

    我试了用delete from ** where ……但是没起作用
    代码;
    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();
      

  9.   

    把commandText打印出来看看
    可能是sql语句的问题
      

  10.   

    果然有问题。
    怎么是elete from Customers where CustomerID='DataGridViewTextBoxCell{ColumnIndex=0,RowIndex=0}'
    我完全没辙了
    不好意思
      

  11.   

    谢谢,问题解决了,请接分~~
    在删除按钮里写
    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();
      

  12.   

    谢谢llm89721630给我的思路及提示,也要谢谢其他各位的帮助,小菜鸟再次谢谢!