0

一意とマークされた2つの文字列列を持つ非常に単純なテーブルがあります。最も基本的なクラッド操作を可能にするためにdatagridviewを接続する方法を誰かに教えてもらえますか?グリッドのみを使用し、追加のコントロールは使用しない予定です。これは、削除操作用のリンク列があることを意味します。

私は、データテーブル、バインディングソース、データアダプター、コマンドビルダーを何百万回も繰り返して試しました。

どうもありがとう。

void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{

}

private void bindingSource_PositionChanged(object sender, EventArgs e)
{
        // if the user moves to a new row, check if the 
        // last row was changed
        BindingSource thisBindingSource =
          (BindingSource)sender;

        DataRow ThisDataRow =
          ((DataRowView)thisBindingSource.Current).Row;
        if (ThisDataRow == _lastDataRow)
        {
            // we need to avoid to write a datarow to the 
            // database when it is still processed. Otherwise
            // we get a problem with the event handling of 
            //the DataTable.
            throw new InvalidOperationException("It seems the" +
              " PositionChanged event was fired twice for" +
              " the same row");
        }

        UpdateRowToDatabase();
        // track the current row for next 
        // PositionChanged event
        _lastDataRow = ThisDataRow;
    }

    private void UpdateRowToDatabase()
    {   
        if (_lastDataRow != null)
        {
            if (_lastDataRow.RowState == DataRowState.Modified
                || _lastDataRow.RowState == DataRowState.Added
                || _lastDataRow.RowState == DataRowState.Deleted)
            {
                DataRow[] rows = new DataRow[1];
                rows[0] = _lastDataRow;
                _dataAdapter.Update(rows);
            }
        }
    }        

void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        Debug.WriteLine(string.Format("Click Col: {0} Row:{1}", e.ColumnIndex, e.RowIndex));

        if (!_edit && e.ColumnIndex == 0)
        {
            if (e.RowIndex < _dataTable.Rows.Count)
            {
                DataRow[] rows = new DataRow[1];
                rows[0] = _dataTable.Rows[e.RowIndex];
                _bindingSource.RemoveCurrent();
                _dataAdapter.Update(rows);
            }
        }
    }
4

1 に答える 1

0

ここでCodePlexプロジェクトのResultSetGridクラスを参照してください:http ://sqlcetoolbox.codeplex.com/SourceControl/changeset/view/77500#1210725

于 2012-04-19T11:04:28.923 に答える