How do you bind SQL Data to a .NET DataGridView?

Posted by Jordan S on Stack Overflow See other posts from Stack Overflow or by Jordan S
Published on 2010-04-08T20:07:39Z Indexed on 2010/04/08 20:33 UTC
Read the original article Hit count: 203

Filed under:
|
|
|

I am trying to bind a table in an SQL database to a DataGridView Control. I would like to make it so that when the user enters a new line of data in the DataGridView that a record is automatically added to the database. Is there a way to do this using LINQ to SQL? I have tried using the code below but after I add a new entry I dont think the data gets added to the DB. Please Help!

        BOMClassesDataContext DB = new BOMClassesDataContext();
        var mfrs = from m in DB.Manufacturers
                   select m;

        BindingSource bs = new BindingSource();
        bs.DataSource = mfrs;

        dataGridView1.DataSource = bs;

I tried adding DB.SubmitChanges() to the CellValueChanged eventhandler and that partially works. If I click the bottom empty row it automatically fills in the ID (identity) column of the table with a "0" instead of the next unused value. If I change that value manually to the next available then it adds the new record fine but if I leave it at 0 it does nothing. How can i fix this?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#