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

2024-02-17

我已经绑定了datagridview with datatable (Growns)。我的主要目标是,用户可以使用datagridview (数据网格视图1),填充和更新数据以及何时button SAVE单击后,所有数据将保存到数据表中,因为我需要它来进行进一步的工作。

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

这是我的代码:

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!");
        }
    }

P.S.:当我手动填写时datatable,在表单打开时datagridview已满,所以datatable and datagridview我想是有联系的...

P.S.2:布尔EmptySpace工作正常。


当你设置this.Update(competitorDataSet.Odrasli); the TableAdapter更新更改自DataTable(新闻、删除、更新的行)到数据库。

自从你打电话以来competitorDataSet.Growns.AcceptChanges(); before TableAdapter.Update,表中的所有更改均已被接受,TableAdapter 无需更新任何内容。

所以只需删除

competitorDataSet.Growns.AcceptChanges();

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么它不将更改从 datagridview 保存到数据表中? 的相关文章

随机推荐