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

时间:2013-07-23 10:18:20

标签: c# winforms datagridview datatable dataadapter

我已将datagridviewdatatable Growns )绑定在一起。我的主要目标是,用户可以使用datagridview dataGridView1 ),填充和更新数据,点击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!");
        }
    }

P.S。:当我手动填写datatable时,表格已打开datagridview已填写,因此我认为datatabledatagridview已关联...

P.S.2。:bool EmptySpace工作正常。

2 个答案:

答案 0 :(得分:2)

当您设置this.Update(competitorDataSet.Odrasli);时,TableAdapter会将更改从DataTable(新闻,已删除,已更新的行)更新为数据库。

由于您在 competitorDataSet.Growns.AcceptChanges();之前致电TableAdapter.Update ,表格中的所有更改都已被接受,而且TableAdapter无需更新。

所以只需删除

competitorDataSet.Growns.AcceptChanges();

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

答案 1 :(得分:0)

您没有使用datagridview编辑数据,您正在使用文本框更改数据集,我认为这是您手动填充的示例...

我将假设您要用于更新数据库的代码从此行开始:

this.dataGridView1.DataSource = competitorDataSet.Growns;

我怀疑您的问题出现在以下代码块中(代码注释中的解释):

//why rebind the datagridview?
//this line should be removed
this.dataGridView1.DataSource = competitorDataSet.Growns;

//why call this here? validation should be done prior 
//to adding the new row to the datatable
//this line should be removed
this.Validate();

this.grownsBindingSource.EndEdit();
if (dataGridView1.BindingContext[competitorDataSet.Growns] != null)
{
dataGridView1.BindingContext[competitorDataSet.Growns].EndCurrentEdit();
}

//reverse the order of these 2 lines
this.grownsTableAdapter.Update(competitorDataSet.Odrasli);
this.grownsTableAdapter.Adapter.AcceptChangesDuringUpdate = true;
/* like this:
this.grownsTableAdapter.Adapter.AcceptChangesDuringUpdate = true;
this.grownsTableAdapter.Update(competitorDataSet.Odrasli);
 */

如果这不能解决您的问题,请发布数据视图的绑定代码。