5

( Growns )datagridviewバインドしました。私の主な目標は、ユーザーが(dataGridView1)を操作してデータを入力および更新できるようにすることです。SA ​​VEをクリックすると、すべてのデータがデータテーブルに保存されます。これは、今後の作業に必要だからです。datatabledatagridviewbutton

データを 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 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 に答える