为什么它不保存从DataGridView变为数据表? [英] Why it doesn't save changes into datatable from datagridview?

查看:130
本文介绍了为什么它不保存从DataGridView变为数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经绑定的datagridview 数据表 Growns 的)。我的主要目标是,用户可以使用 datagridview的工作 dataGridView1 的),填充和更新数据,并在按钮保存的点击,所有的数据将被保存到数据表,因为我需要它为进一步开展工作。

I have binded datagridview with datatable (Growns). My main goal is, that user can work with datagridview (dataGridView1), filling and updating data and when button SAVE is clicked, all data would be saved into datatable, because I need it for further work.

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

Everything works fine, exept saving data into datatable. What am I doing wrong?

下面是我的代码:

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



PS:当我手工填写数据表,在表单打开的datagridview 被填满,所以数据表的datagridview 连接我想...

P.S.: When I manually fill datatable, on form open datagridview is filled, so datatable and datagridview are connected I suppose...

PS2:BOOL EmptySpace 正常工作

P.S.2.: bool EmptySpace works fine.

推荐答案

当您设置 this.Update(competitorDataSet.Odrasli); 的TableAdapter 更新从数据表(新闻,删除,更新的行)更改到数据库。

When you set this.Update(competitorDataSet.Odrasli); the TableAdapter updates the changes from DataTable (news, deleted, updated rows) to the database.

既然你叫 competitorDataSet.Growns.AcceptChanges(); 之前 TableAdapter.Update ,表中的所有修改都已经接受的TableAdapter没有任何更新。

Since you call competitorDataSet.Growns.AcceptChanges(); before TableAdapter.Update, all changes in the table are already accepted and TableAdapter has nothing to update.

所以只要删除

competitorDataSet.Growns.AcceptChanges();



此外,如果您设置 this.grownsTableAdapter.Adapter.AcceptChangesDuringUpdate = TRUE 之前 grownsTableAdapter.Update(competitorDataSet.Odrasli); ,更改将被接受,所以你不需要接受改变自己(和它在我看来,默认值是true,所以我不知道需要此线)

Also, if you set this.grownsTableAdapter.Adapter.AcceptChangesDuringUpdate = true before grownsTableAdapter.Update(competitorDataSet.Odrasli);, the changes will be accepted and so you don't need to accept changes yourself (and it seems to me that default value is True so I am not sure this line is required)

这篇关于为什么它不保存从DataGridView变为数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆