为什么它不将更改从 datagridview 保存到数据表中?

use*_*094 5 c# datatable datagridview dataadapter winforms

我已经绑定datagridviewdatatableGrowns)。我的主要目标是,该用户可以使用datagridviewdataGridView1),填充和更新数据,当单击button SAVE时,所有数据都将保存到数据表中,因为我需要它进行进一步的工作。

一切正常,除了将数据保存到 datatable。我究竟做错了什么?

这是我的代码:

private void Form2_Load(object sender, EventArgs e) {
        // TODO: This line of code loads data into the 'tekmovalecDataSet.Odrasli' table. You can move, or remove it, as needed.
        this.grownsTableAdapter.Fill(this.competitorDataSet.Odrasli);
    }

private void buttonSave_Click(object sender, EventArgs e) {
        if (EmptySpace())
        {
                CompetitorDataSet.OdrasliRow newGrownsRow = competitorDataSet.Growns.NewGrownsRow();
                newGrownsRow.StN = textStN.Text;
                newGrownsRow.Name = textN.Text;
                newGrownsRow.Surname = textSN.Text;
                newGrownsRow.Club = textC.Text;
                newGrownsRow.YBirth = textYB.Text;
                competitorDataSet.Growns.Rows.Add(OdrasliNova);
                competitorDataSet.Growns.AcceptChanges();

                this.dataGridView1.DataSource = competitorDataSet.Growns;
                this.Validate();
                this.grownsBindingSource.EndEdit();
                if (dataGridView1.BindingContext[competitorDataSet.Growns] != null)
                {
                    dataGridView1.BindingContext[competitorDataSet.Growns].EndCurrentEdit();
                }
                this.grownsTableAdapter.Update(competitorDataSet.Odrasli);
                this.grownsTableAdapter.Adapter.AcceptChangesDuringUpdate = true;
        }
        else
        {
            MessageBox.Show("Fill ALL data about competitor!");
        }
    }
Run Code Online (Sandbox Code Playgroud)

PS:当我手动填写datatable,表格上的开放datagridview充满,所以datatabledatagridview连接我想...

PS2.: boolEmptySpace工作正常。

Chr*_*ris 2

当您设置this.Update(competitorDataSet.Odrasli);更新时TableAdapter,更改来自DataTable时,(新闻、删除、更新的行)到数据库的

由于您调用competitorDataSet.Growns.AcceptChanges(); before TableAdapter.Update,表中的所有更改都已被接受,并且 TableAdapter 没有任何可更新的内容。

所以只需删除

competitorDataSet.Growns.AcceptChanges();
Run Code Online (Sandbox Code Playgroud)

this.grownsTableAdapter.Adapter.AcceptChangesDuringUpdate = true 另外,如果您在之前 设置grownsTableAdapter.Update(competitorDataSet.Odrasli);,则更改将被接受,因此您不需要自己接受更改(在我看来,默认值为 True,所以我不确定是否需要此行)