简体   繁体   中英

Linq to SQL: Updating dataGridView after SubmitChanges isn't working

First time I've worked with LINQ, and I've encountered a strange problem. I'm using MS SQL 2008. If I alter a row and SubmitChanges(), I can see the changes in the SQL Server Management Studio, but when I query to refill my dataGridViews with the updated information, I get the old version of the data.

Below is a change in my data:

        if (textBox1.Text.Length > 1)
        {
            var query = from eq in db.equipments
                        where eq.SerNo == serial
                        select eq;

            foreach (equipment equip in query)
            {
                equip.Loc = textBox1.Text;
                equip.Owner = "Evisive";
                equip.Status = updatedStatus;
                equip.SystemName = "";
            }

            db.SubmitChanges();
        }

        else
        {
            MessageBox.Show("Enter a Destination");
        }

Again, that part works (changes the data in the database). Below is where I try to update the table (the part that doesn't seem to work):

    public void Update_EquipmentGrid()
    {
        BindingSource b = new BindingSource();
        b.DataSource = from eq in db.equipments
                       select eq;
        dataGridView2.DataSource = b;
    }

Is this not the right way to update a table?

Figured it out. This link explains it.

http://msdn.microsoft.com/en-us/library/Bb386982(v=vs.110).aspx

Code should be:

        InventoryDataContext dba = new InventoryDataContext();

        BindingSource b = new BindingSource();
        b.DataSource = from eq in dba.equipments
                       select eq;

        dataGridView2.DataSource = b;

Not sure why, but you need to create a new instance of the DataContext each time it is updated.

Try

dataGridView2.DataSource = null;

before

dataGridView2.DataSource = b;

Also try converting your data to list before assigning.

b.DataSource = (from eq in db.equipments
                select eq).ToList();

maybe this helps you

https://www.devexpress.com/Support/Center/Question/Details/Q142798

example:

var orders = DBInterface.dcAPI.OrderInfos;
var b = orders.GetNewBindingList();
dgOrders.ItemsSource = b;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM