开发者

.NET 3.5 DataGridView Wont Save To Database

开发者 https://www.devze.com 2022-12-25 23:58 出处:网络
I have created a test Winforms application in Visual Studio 2008 (SP1) to see just how \"RAD\" C# and .NET 3.5 can be. So far I have mixed emotions.

I have created a test Winforms application in Visual Studio 2008 (SP1) to see just how "RAD" C# and .NET 3.5 can be. So far I have mixed emotions.

  1. Added a service-based database to my application (MyDB.mdf) and added two tables - Contact (id [identity], name [varchar] and number [varchar] columns) and Group (id [identity] and name [varchar] columns)

  2. Added a DataSource, selected "Database" as the source, used the default connection string as the connection (which points to my database) and selected "All Tables" to be included in the data source and saved as MyDBDataSet

  3. Expanded the data source showing my two tables, selected the "Group" table and chose to display it as a DataGridView (from the dropdown option on the right of each entity) and dragged it onto Form1, thus creating a groupBindingNavigator, groupBindingSource, groupTableAdapter, tableAdapterManager, myDBDataset and groupDataGridView

  4. Press F5 to test the application, enter the name "Test" under the DataGridView's "name" column and click "Save" on the navigator which has autogenerated code to save the data that looks like this:

    private vo开发者_开发技巧id groupBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
        this.Validate();
        this.groupBindingSource.EndEdit();
        this.tableAdapterManager.UpdateAll(this.myDBDataSet);
    }
    
  5. Stop the application and have a look at the database data, you will see no data saved there in the "Group" table. I dont know why and cant find out how to fix it! Googled for about 30 minutes with no luck.

The code is auto-generated with the controls, so you'd think that it would work too :)


Look closely in your folders. You will probably find the original Database in the Project (source) folder and a copy in the Bin\Debug folder. On each build the copy is overwritten. You can control this with the File Properties in Solution Explorer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号