无法保存对数据集的更改

时间:2014-11-20 21:27:49

标签: c# .net datagridview dataset datasource

我有一个dataSource添加到我的C#项目中,它只有几列,用户将输入数据并向数据库添加行,并且还能够搜索某些行。我无法保存任何更改。当我按下添加按钮时,我的DataGridView中出现了更改,但是当我重新启动程序时(我构建并以btw方式导出它,而不是在调试模式下),之前的更改不存在。这是为什么?

 //called when the new entry button is clicked, adds new row into DB
    private void CreatEnrtyBtn_Click(object sender, EventArgs e)
    {
        //Create new row to construct
        BettingDB1DataSet.HorsesRow row = bettingDB1DataSet.Horses.NewHorsesRow();

        //Add values to row
        row.HorseName = this.HorseNameBox.Text;
        row.Trainer = this.TrainerNameBox.Text;
        row.Place = this.PlaceBox.Text;
        row.Location = this.LocationBox.Text;
        row.Jockey = this.JockeyNameBox.Text;
        row.Track = this.LocationBox.Text;
        row.Ground = "temp";

        //Add row to DB and commit changes
        bettingDB1DataSet.Horses.AddHorsesRow(row);

        //tried both of these on their own, but neither work?
        horsesTableAdapter.Update(bettingDB1DataSet.Horses);
        horsesTableAdapter.Update(row);
    }

如何根据要求创建horsesTableAdapter:

        private BettingDB1DataSetTableAdapters.HorsesTableAdapter horsesAdapter;


    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'bettingDB1DataSet2.Horses' table. You can move, or remove it, as needed.
        this.horsesTableAdapter.Fill(this.bettingDB1DataSet2.Horses);
        this.dataGridView1.DataSource = bettingDB1DataSet.Horses;

    }

2 个答案:

答案 0 :(得分:0)

您在更新之前是否接受数据集更改???。

bettingDB1DataSet.Acceptchanges();         horsesTableAdapter.Update(bettingDB1DataSet.Horses);

答案 1 :(得分:0)

  1. 您使用dataadapter填充bettingDB1DataSet2。
  2. 您的网格数据源调用bettingDB1DataSet。
  3. 您正在更新bettingDB1DataSet。
  4. 也许这就是问题。