如何保存对DataGridView所做的更改?

时间:2014-06-15 20:32:03

标签: c# datagridview dataset

我以为我知道怎么做,但当我实际上这样做时,似乎我不知道该怎么做。

我想要实现的是在单击搜索btn时显示数据集中已过滤的表行。允许用户在datagridview上进行更改,或者在单击Update btn时使用更新面板上文本框中提供的值更新所选行。但我似乎无法做到这一点。

 private void Display()
    {
        bindingSourceDisplay.DataSource=_dsNorthwind.Customers.Where(x=>!x.IsRegionNull()                         && !x.IsFaxNull()                                                                         && !x.IsPostalCodeNull()).Take(10);

        dgvSearchResult.DataSource = bindingSourceDisplay;
    }

 internal void UpdateSelectedRows(UpdateBtnClickedEventArgs e)
    {
        //cannot do this because datagridviewselectedrow and customerrow are a type mismatch
        foreach (DS_Northwind.CustomersRow selectedRow in dgvSearchResult.SelectedRows)
        {
            selectedRow.Phone = e.PhoneNum;
            selectedRow.PostalCode = e.PostalCode;
        }
//how do I update each selected rows in datagridview? Can't do selectedRow.Cells["ColName"] either for some reason...
//if my dataset is connected to my database, isn't saving changes as simple as doing accept changes?
            _dsNorthwind.AcceptChanges();
        }

1 个答案:

答案 0 :(得分:0)

看来你错过了一个DataAdapter对象。看看这篇文章:

Updating Database using Datagrid in C#