Winforms DataGridView SQL compact CRUD示例

Winforms DataGridView SQL compact CRUD示例,winforms,datagridview,sql-server-ce,Winforms,Datagridview,Sql Server Ce,我有一个非常简单的表,有两个字符串列标记为unique。谁能告诉我如何连接datagridview以允许最基本的crud操作。我打算只使用网格,不使用其他控件。这意味着有一个用于删除操作的链接列 我已经尝试了一百万次迭代来创建数据表、绑定源、数据适配器和命令生成器 非常感谢 void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) { } private void bindingSource_P

我有一个非常简单的表,有两个字符串列标记为unique。谁能告诉我如何连接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);
            }
        }
    }
void dataGridView1\u DataError(对象发送方,DataGridViewDataErrorEventArgs e)
{
}
私有void bindingSource\u位置已更改(对象发送方,事件参数e)
{
//如果用户移动到新行,请检查
//最后一行已更改
BindingSource此BindingSource=
(源)发送方;
DataRow ThisDataRow=
((DataRowView)thisBindingSource.Current).Row;
如果(ThisDataRow==\u lastDataRow)
{
//我们需要避免将数据行写入
//当数据库仍在处理时。否则
//我们的事件处理有问题
//数据表。
抛出新的InvalidOperationException(“似乎是”+
“已为触发两次PositionChanged事件”+
“同一行”);
}
UpdateRowToDatabase();
//跟踪当前行以获取下一行
//位置更改事件
_lastDataRow=此DataRow;
}
私有void UpdateRowToDatabase()
{   
如果(_lastDataRow!=null)
{
如果(_lastDataRow.RowState==DataRowState.Modified
||_lastDataRow.RowState==DataRowState.Added
||_lastDataRow.RowState==DataRowState.Deleted)
{
DataRow[]行=新DataRow[1];
行[0]=\u lastDataRow;
_dataAdapter.Update(行);
}
}
}        
void dataGridView1\u CellClick(对象发送者,DataGridViewCellEventArgs e)
{
WriteLine(string.Format(“单击列:{0}行:{1}”,e.ColumnIndex,e.RowIndex));
如果(!\u编辑(&e.ColumnIndex==0)
{
if(e.RowIndex<\u dataTable.Rows.Count)
{
DataRow[]行=新DataRow[1];
行[0]=_dataTable.rows[e.RowIndex];
_bindingSource.RemoveCurrent();
_dataAdapter.Update(行);
}
}
}

在这里查看CodePlex项目中的ResultSetGrid类:

这让我前进了很多,谢谢。我仍然有删除记录的问题。您的表必须有一个主键-是吗?是的,我有一个主键,最后我使用了导航器控件,它上面有一个删除按钮。