5

我已datagridviewdatatable( Growns ) 绑定。我的主要目标是,该用户可以使用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!");
        }
    }

PS:当我手动填写时datatable,填写表格打开,datagridview所以我想是连接的......datatabledatagridview

PS2.: boolEmptySpace工作正常。

4

2 回答 2

2

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

由于您之前调用competitorDataSet.Growns.AcceptChanges(); TableAdapter.Update,因此表中的所有更改都已被接受,并且 TableAdapter 没有需要更新的内容。

所以只需删除

competitorDataSet.Growns.AcceptChanges();

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

于 2013-07-24T09:03:30.437 回答
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);
 */

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

于 2013-07-23T21:30:17.017 回答