DataGridView and Generics
-
Hello to everybody, I've got a problem with datagridview and generics, here's the piece of code :
dataGridView1.ReadOnly = false; List myProds = new List(); myProds.Add(new Product("Prod 1", 1, 1)); myProds.Add(new Product("Prod 2", 2, 2)); myProds.Add(new Product("Prod 3", 3, 3)); dataGridView1.DataSource = myProds; myProds.Add(new Product("Prod 99", 99, 99));
on video I've 3 rows but in the datagrid.datasource I've 4 items. Where do I do wrong? Thanks in advance Paolo Ponzano -
Hello to everybody, I've got a problem with datagridview and generics, here's the piece of code :
dataGridView1.ReadOnly = false; List myProds = new List(); myProds.Add(new Product("Prod 1", 1, 1)); myProds.Add(new Product("Prod 2", 2, 2)); myProds.Add(new Product("Prod 3", 3, 3)); dataGridView1.DataSource = myProds; myProds.Add(new Product("Prod 99", 99, 99));
on video I've 3 rows but in the datagrid.datasource I've 4 items. Where do I do wrong? Thanks in advance Paolo PonzanoAs this is windows forms, there's no databind command, the grid is bound at the point that you set the data source. So, the objects you add after, are there, but the grid does not rebind every time you add an item.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
As this is windows forms, there's no databind command, the grid is bound at the point that you set the data source. So, the objects you add after, are there, but the grid does not rebind every time you add an item.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
so what should I do? Thanks Paolo
-
so what should I do? Thanks Paolo
Reset the datasource after you have added the new items.
Deja View - the feeling that you've seen this post before.
-
Reset the datasource after you have added the new items.
Deja View - the feeling that you've seen this post before.
First of all, thanks to all of you for your answe, I've resolved it using BindingList instead of List, now I've a problem of cross-threading but I'm gonna take care of it! Thanks again Paolo
-
First of all, thanks to all of you for your answe, I've resolved it using BindingList instead of List, now I've a problem of cross-threading but I'm gonna take care of it! Thanks again Paolo
ok, now that the problem has been fixed we've another one that came out, and for this I need your help at all!!! This is the main form event :
private void Form1_Load(object sender, EventArgs e) { myWorker = new Worker(); dataGridView1.ReadOnly = false; dataGridView1.DataSource = myWorker.myList; Thread myThread = new Thread(new ThreadStart(myWorker.DoWork)); myThread.Start(); }
The Worker class is this one :public class Worker { public BindingList myList = new BindingList(); public void DoWork() { Random r = new Random(); for(int i=0; i<1000; i++) { myList.Add(new Product("Test",r.Next(),r.Next())); Thread.Sleep(1000); } } }
At themyList.Add(...)
I get an exception of type "Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on." But this happen in a Business Object not in a Window's Control. I suppose this's because of the datagridview.Datasource property set, but how can I fix it? Thanks in advance, Bests