0

OleDBConnection と Datagridview1 コンポーネントに接続された、アクセス可能なテーブル Movies があります。そのようなコードがあります:

if (this.dataGridView1.SelectedRows.Count > 0)
        {
            string queryString = "SELECT movieID, Title, MovieYear, Country,Located, Description, Poster, Actors, FilmDirector, Type FROM Movie,movieType WHERE movietype.typeID = Movie.typeID";
            foreach (DataGridViewRow dgvrCurrent in dataGridView1.SelectedRows)
            {
                 int currentRow = int.Parse(dataGridView1.CurrentCell.RowIndex.ToString());
                try
                {
                    string movieIDString = dataGridView1[0, currentRow].Value.ToString();
                    movieIDInt = int.Parse(movieIDString);                

                    string queryDeleteString = "DELETE FROM Movie where movieID = " + movieIDInt + ";";
                    OleDbCommand sqlDelete = new OleDbCommand();
                    sqlDelete.CommandText = queryDeleteString;
                    sqlDelete.Connection = database;
                    sqlDelete.ExecuteNonQuery();
                    loadDataGrid(queryString);
                 }
                catch (Exception ex) { }


            }

        }

選択した複数の行を削除する必要がありますが、そうではありません:(

私の間違いはどこですか、助けてもらえますか?

4

1 に答える 1

1

loadDataGrid(queryString) は for ループの外にある必要があると思います。ただの推測ですが定かではありません。

if (this.dataGridView1.SelectedRows.Count > 0)
    {
        string queryString = "SELECT movieID, Title, MovieYear, Country,Located, Description, Poster, Actors, FilmDirector, Type FROM Movie,movieType WHERE movietype.typeID = Movie.typeID";
        foreach (DataGridViewRow dgvrCurrent in dataGridView1.SelectedRows)
        {
             int currentRow = int.Parse(dataGridView1.CurrentCell.RowIndex.ToString());
            try
            {
                string movieIDString = dataGridView1[0, currentRow].Value.ToString();
                movieIDInt = int.Parse(movieIDString);                

                string queryDeleteString = "DELETE FROM Movie where movieID = " + movieIDInt + ";";
                OleDbCommand sqlDelete = new OleDbCommand();
                sqlDelete.CommandText = queryDeleteString;
                sqlDelete.Connection = database;
                sqlDelete.ExecuteNonQuery();                    
             }
            catch (Exception ex) { }
        }

        loadDataGrid(queryString);
    }
于 2012-04-05T19:35:08.397 に答える