简体   繁体   English

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

[英]How to save changes made to DataGridView?

I thought I knew how to do this, but when I am actually doing it, doesn't seem like I know how to do this.我以为我知道如何做到这一点,但是当我真正做到这一点时,似乎我不知道如何做到这一点。

What I am trying to achieve is display a filtered rows of table that is in dataset when search btn is clicked.我想要实现的是在单击搜索 btn 时显示数据集中的表的过滤行。 Allow users to make changes on the datagridview or update selected rows with values provided on textbox on the update panel when Update btn is clicked.当单击更新 btn 时,允许用户对 datagridview 进行更改或使用更新面板上文本框中提供的值更新选定的行。 But I can't seem to achieve this.但我似乎无法做到这一点。

 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();
        }

It seems you are missing a DataAdapter obejct.您似乎缺少一个DataAdapter对象。

Look at this post:看看这个帖子:

Updating Database using Datagrid in C# 在 C# 中使用 Datagrid 更新数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM