0

我有一个非常简单的表,其中有两个标记为唯一的字符串列。谁能告诉我如何连接 datagridview 以允许最基本的 crud 操作。我打算只使用网格而不使用其他控件。这意味着有一个用于删除操作的链接列。

我已经尝试过一百万次迭代,包括数据表、绑定源、数据适配器、命令构建器……

非常感谢。

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 回答